Switch launcher to use PG 10.
[discourse_docker.git] / templates / postgres.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/service/postgres/log/run
42 chmod: "+x"
43 contents: |
44 #!/bin/sh
45 mkdir -p /var/log/postgres
46 exec svlogd /var/log/postgres
47
48
49 - file:
50 path: /etc/runit/3.d/99-postgres
51 chmod: "+x"
52 contents: |
53 #!/bin/bash
54 sv stop postgres
55
56 - file:
57 path: /root/upgrade_postgres
58 chmod: "+x"
59 contents: |
60 #!/bin/bash
61 PG_MAJOR_OLD=`cat /shared/postgres_data/PG_VERSION`
62
63 if [ ! "10" = "$PG_MAJOR_OLD" ]; then
64 echo Upgrading PostgreSQL to from version ${PG_MAJOR_OLD} to 10
65 rm -fr /shared/postgres_data_new
66 install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/10/bin/initdb -D /shared/postgres_data_new || exit 0
67 apt-get update
68 apt-get install -y postgresql-${PG_MAJOR_OLD}
69 chown -R postgres:postgres /var/lib/postgresql/10
70 /etc/init.d/postgresql stop
71 rm -fr /shared/postgres_data/postmaster.pid
72 cd ~postgres
73 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
74 echo >> /shared/postgres_data/postgresql.conf
75 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
76 SUCCESS=true
77 sudo -u postgres /usr/lib/postgresql/10/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/${PG_MAJOR_OLD}/bin -B /usr/lib/postgresql/10/bin || SUCCESS=false
78
79 if [[ "$SUCCESS" == 'false' ]]; then
80 echo -------------------------------------------------------------------------------------
81 echo UPGRADE OF POSTGRES FAILED
82 echo
83 echo You are going to need to export your data and import into a clean instance:
84 echo
85
86 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
87 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml"'
88 echo
89 fi
90
91 echo Run "./launcher rebuild app" again
92 echo
93 echo When your instance is running:
94 echo Run "./launcher enter app"
95 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
96 echo
97
98 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
99 echo Undo the base_image in your container config
100 fi
101
102 echo Run: "./launcher stop app"
103 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
104 echo Run: "./launcher rebuild app"
105 echo
106 echo Run: "./launcher enter app"
107 echo Run: "cd /shared/postgres_backup"
108 echo Run: "sv stop unicorn"
109 echo Run: "sudo -iu postgres dropdb discourse"
110 echo Run: "sudo -iu postgres createdb discourse"
111 echo Run: "sudo -iu postgres psql discourse < backup.db"
112 echo Run: "exit"
113 echo Run: "./launcher rebuild app"
114 exit 1
115 fi
116
117 mv /shared/postgres_data /shared/postgres_data_old
118 mv /shared/postgres_data_new /shared/postgres_data
119
120 echo -------------------------------------------------------------------------------------
121 echo UPGRADE OF POSTGRES COMPLETE
122 echo
123 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
124 echo
125 echo To complete the upgrade, rebuild again using:
126 echo
127 echo ./launcher rebuild app
128 echo -------------------------------------------------------------------------------------
129 # Magic exit status to denote no failure
130 exit 77
131 fi
132
133 - exec:
134 cmd:
135 - chown -R root /var/lib/postgresql/10/main
136 - "[ ! -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"
137 - chown -R postgres:postgres /shared/postgres_data
138 - chown -R postgres:postgres /var/run/postgresql
139
140 - exec: /root/upgrade_postgres
141 - exec: rm /root/upgrade_postgres
142
143 - replace:
144 filename: "/etc/postgresql/10/main/postgresql.conf"
145 from: "data_directory = '/var/lib/postgresql/10/main'"
146 to: "data_directory = '/shared/postgres_data'"
147
148 # listen on all interfaces
149 - replace:
150 filename: "/etc/postgresql/10/main/postgresql.conf"
151 from: /#?listen_addresses *=.*/
152 to: "listen_addresses = '*'"
153
154 # sync commit off is faster and less spiky, also marginally less safe
155 - replace:
156 filename: "/etc/postgresql/10/main/postgresql.conf"
157 from: /#?synchronous_commit *=.*/
158 to: "synchronous_commit = $db_synchronous_commit"
159
160 # default is 128MB which is way too small
161 - replace:
162 filename: "/etc/postgresql/10/main/postgresql.conf"
163 from: /#?shared_buffers *=.*/
164 to: "shared_buffers = $db_shared_buffers"
165
166 # default is 1MB which is too small
167 - replace:
168 filename: "/etc/postgresql/10/main/postgresql.conf"
169 from: /#?work_mem *=.*/
170 to: "work_mem = $db_work_mem"
171
172 # allow for other
173 - replace:
174 filename: "/etc/postgresql/10/main/postgresql.conf"
175 from: /#?default_text_search_config *=.*/
176 to: "default_text_search_config = '$db_default_text_search_config'"
177
178 # Necessary to enable backups
179 - exec:
180 cmd:
181 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
182
183 - replace:
184 filename: "/etc/postgresql/10/main/postgresql.conf"
185 from: /#?max_wal_senders *=.*/
186 to: "max_wal_senders = $db_max_wal_senders"
187
188 - replace:
189 filename: "/etc/postgresql/10/main/postgresql.conf"
190 from: /#?wal_level *=.*/
191 to: "wal_level = $db_wal_level"
192
193 - replace:
194 filename: "/etc/postgresql/10/main/postgresql.conf"
195 from: /#?checkpoint_segments *=.*/
196 to: "checkpoint_segments = $db_checkpoint_segments"
197
198 - replace:
199 filename: "/etc/postgresql/10/main/postgresql.conf"
200 from: /#?logging_collector *=.*/
201 to: "logging_collector = $db_logging_collector"
202
203 - replace:
204 filename: "/etc/postgresql/10/main/postgresql.conf"
205 from: /#?log_min_duration_statement *=.*/
206 to: "log_min_duration_statement = $db_log_min_duration_statement"
207
208 - replace:
209 filename: "/etc/postgresql/10/main/pg_hba.conf"
210 from: /^#local +replication +postgres +peer$/
211 to: "local replication postgres peer"
212
213 # allow all to connect in with md5 auth
214 - replace:
215 filename: "/etc/postgresql/10/main/pg_hba.conf"
216 from: /^host.*all.*all.*127.*$/
217 to: "host all all 0.0.0.0/0 md5"
218
219 - exec:
220 background: true
221 # use fast shutdown for pg
222 stop_signal: INT
223 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
224
225 # give db a few secs to start up
226 - exec: "sleep 5"
227
228 - exec: su postgres -c 'createdb $db_name' || true
229 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
230 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
231 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
232 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
233 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
234 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
235 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
236 - exec:
237 stdin: |
238 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
239 cmd: sudo -u postgres psql $db_name
240 raise_on_fail: false
241
242 - file:
243 path: /var/lib/postgresql/take-database-backup
244 chown: postgres:postgres
245 chmod: "+x"
246 contents: |
247 #!/bin/bash
248 ID=db-$(date +%F_%T)
249 FILENAME=/shared/postgres_backup/$ID.tar.gz
250 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
251 echo $FILENAME
252
253 - file:
254 path: /var/spool/cron/crontabs/postgres
255 contents: |
256 # m h dom mon dow command
257 #MAILTO=?
258 #0 */4 * * * /var/lib/postgresql/take-database-backup
259
260 - exec:
261 hook: postgres
262 cmd: "echo postgres installed!"