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