FIX: disk space check tests /shared
[discourse_docker.git] / templates / postgres.10.template.yml
1 params:
2 db_synchronous_commit: "off"
3 db_shared_buffers: "256MB"
4 db_work_mem: "10MB"
5 db_default_text_search_config: "pg_catalog.english"
6 db_name: discourse
7 db_user: discourse
8 db_wal_level: minimal
9 db_max_wal_senders: 0
10 db_checkpoint_segments: 6
11 db_logging_collector: off
12 db_log_min_duration_statement: 100
13
14 hooks:
15 before_code:
16 - replace:
17 filename: /etc/service/unicorn/run
18 from: "# postgres"
19 to: sv start postgres || exit 1
20
21 run:
22 - exec: mkdir -p /shared/postgres_run
23 - exec: chown postgres:postgres /shared/postgres_run
24 - exec: chmod 775 /shared/postgres_run
25 - exec: rm -fr /var/run/postgresql
26 - exec: ln -s /shared/postgres_run /var/run/postgresql
27 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
28 - exec: rm -fr /shared/postgres_run/.s*
29 - exec: rm -fr /shared/postgres_run/*.pid
30 - exec: mkdir -p /shared/postgres_run/10-main.pg_stat_tmp
31 - exec: chown postgres:postgres /shared/postgres_run/10-main.pg_stat_tmp
32 - file:
33 path: /etc/service/postgres/run
34 chmod: "+x"
35 contents: |
36 #!/bin/sh
37 exec 2>&1
38 HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main
39
40 - file:
41 path: /etc/runit/3.d/99-postgres
42 chmod: "+x"
43 contents: |
44 #!/bin/bash
45 sv stop postgres
46
47 - exec:
48 cmd:
49 - chown -R root /var/lib/postgresql/10/main
50 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/10/bin/initdb -D /shared/postgres_data || exit 0"
51 - chown -R postgres:postgres /shared/postgres_data
52 - chown -R postgres:postgres /var/run/postgresql
53
54 - replace:
55 filename: "/etc/postgresql/10/main/postgresql.conf"
56 from: "data_directory = '/var/lib/postgresql/10/main'"
57 to: "data_directory = '/shared/postgres_data'"
58
59 # listen on all interfaces
60 - replace:
61 filename: "/etc/postgresql/10/main/postgresql.conf"
62 from: /#?listen_addresses *=.*/
63 to: "listen_addresses = '*'"
64
65 # sync commit off is faster and less spiky, also marginally less safe
66 - replace:
67 filename: "/etc/postgresql/10/main/postgresql.conf"
68 from: /#?synchronous_commit *=.*/
69 to: "synchronous_commit = $db_synchronous_commit"
70
71 # default is 128MB which is way too small
72 - replace:
73 filename: "/etc/postgresql/10/main/postgresql.conf"
74 from: /#?shared_buffers *=.*/
75 to: "shared_buffers = $db_shared_buffers"
76
77 # default is 1MB which is too small
78 - replace:
79 filename: "/etc/postgresql/10/main/postgresql.conf"
80 from: /#?work_mem *=.*/
81 to: "work_mem = $db_work_mem"
82
83 # allow for other
84 - replace:
85 filename: "/etc/postgresql/10/main/postgresql.conf"
86 from: /#?default_text_search_config *=.*/
87 to: "default_text_search_config = '$db_default_text_search_config'"
88
89 # Necessary to enable backups
90 - exec:
91 cmd:
92 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
93
94 - replace:
95 filename: "/etc/postgresql/10/main/postgresql.conf"
96 from: /#?max_wal_senders *=.*/
97 to: "max_wal_senders = $db_max_wal_senders"
98
99 - replace:
100 filename: "/etc/postgresql/10/main/postgresql.conf"
101 from: /#?wal_level *=.*/
102 to: "wal_level = $db_wal_level"
103
104 - replace:
105 filename: "/etc/postgresql/10/main/postgresql.conf"
106 from: /#?checkpoint_segments *=.*/
107 to: "checkpoint_segments = $db_checkpoint_segments"
108
109 - replace:
110 filename: "/etc/postgresql/10/main/postgresql.conf"
111 from: /#?logging_collector *=.*/
112 to: "logging_collector = $db_logging_collector"
113
114 - replace:
115 filename: "/etc/postgresql/10/main/postgresql.conf"
116 from: /#?log_min_duration_statement *=.*/
117 to: "log_min_duration_statement = $db_log_min_duration_statement"
118
119 - replace:
120 filename: "/etc/postgresql/10/main/pg_hba.conf"
121 from: /^#local +replication +postgres +peer$/
122 to: "local replication postgres peer"
123
124 # allow all to connect in with md5 auth
125 - replace:
126 filename: "/etc/postgresql/10/main/pg_hba.conf"
127 from: /^host.*all.*all.*127.*$/
128 to: "host all all 0.0.0.0/0 md5"
129
130 - exec:
131 background: true
132 # use fast shutdown for pg
133 stop_signal: INT
134 cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main
135
136 # give db a few secs to start up
137 - exec: "sleep 5"
138
139 - exec: su postgres -c 'createdb $db_name' || true
140 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
141 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
142 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
143 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
144 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
145 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
146 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
147 - exec:
148 stdin: |
149 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
150 cmd: sudo -u postgres psql $db_name
151 raise_on_fail: false
152
153 - file:
154 path: /var/lib/postgresql/take-database-backup
155 chown: postgres:postgres
156 chmod: "+x"
157 contents: |
158 #!/bin/bash
159 ID=db-$(date +%F_%T)
160 FILENAME=/shared/postgres_backup/$ID.tar.gz
161 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
162 echo $FILENAME
163
164 - file:
165 path: /var/spool/cron/crontabs/postgres
166 contents: |
167 # m h dom mon dow command
168 #MAILTO=?
169 #0 */4 * * * /var/lib/postgresql/take-database-backup
170
171 - exec:
172 hook: postgres
173 cmd: "echo postgres installed!"