Bump base image (#538)
[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: locale-gen $LANG && update-locale
23 - exec: mkdir -p /shared/postgres_run
24 - exec: chown postgres:postgres /shared/postgres_run
25 - exec: chmod 775 /shared/postgres_run
26 - exec: rm -fr /var/run/postgresql
27 - exec: ln -s /shared/postgres_run /var/run/postgresql
28 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
29 - exec: rm -fr /shared/postgres_run/.s*
30 - exec: rm -fr /shared/postgres_run/*.pid
31 - exec: mkdir -p /shared/postgres_run/12-main.pg_stat_tmp
32 - exec: chown postgres:postgres /shared/postgres_run/12-main.pg_stat_tmp
33 - file:
34 path: /etc/service/postgres/run
35 chmod: "+x"
36 contents: |
37 #!/bin/sh
38 exec 2>&1
39 HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/12/bin/postmaster -D /etc/postgresql/12/main
40
41 - file:
42 path: /etc/service/postgres/log/run
43 chmod: "+x"
44 contents: |
45 #!/bin/sh
46 mkdir -p /var/log/postgres
47 exec svlogd /var/log/postgres
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 [ ! "12" = "$PG_MAJOR_OLD" ]; then
64 echo Upgrading PostgreSQL from version ${PG_MAJOR_OLD} to 12
65 free_disk=$(df -P -B1 /shared | tail -n 1 | awk '{print $4}')
66 required=$(($(du -sb /shared/postgres_data | awk '{print $1}') * 2))
67
68 if [ "$free_disk" -lt "$required" ]; then
69 echo "WARNING: Upgrading PostgresSQL would require an addtional $(numfmt --to=si $(($required - $free_disk))) of disk space"
70 echo "Please free up some space, or expand your disk, before continuing."
71 echo ''
72 echo 'To avoid upgrading change "templates/postgres.template.yml" TO "templates/postgres.10.template.yml" in containers/app.yml'
73 exit 1
74 fi
75
76 if [ -d /shared/postgres_data_old ]; then
77 mv /shared/postgres_data_old /shared/postgres_data_older
78 fi
79
80 rm -fr /shared/postgres_data_new
81 install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/12/bin/initdb -D /shared/postgres_data_new || exit 0
82 apt-get update
83 apt-get install -y postgresql-${PG_MAJOR_OLD}
84 chown -R postgres:postgres /var/lib/postgresql/12
85 /etc/init.d/postgresql stop
86 rm -fr /shared/postgres_data/postmaster.pid
87 cd ~postgres
88 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
89 echo >> /shared/postgres_data/postgresql.conf
90 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
91 SUCCESS=true
92 sudo -u postgres /usr/lib/postgresql/12/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/${PG_MAJOR_OLD}/bin -B /usr/lib/postgresql/12/bin || SUCCESS=false
93
94 if [[ "$SUCCESS" == 'false' ]]; then
95 echo -------------------------------------------------------------------------------------
96 echo UPGRADE OF POSTGRES FAILED
97 echo
98 echo Please visit https://meta.discourse.org/t/postgresql-12-update/151236 for support
99 echo
100 echo You can run "./launcher start app" to restart your app in the meanwhile
101 echo
102 exit 1
103 fi
104
105 mv /shared/postgres_data /shared/postgres_data_old
106 mv /shared/postgres_data_new /shared/postgres_data
107
108 echo -------------------------------------------------------------------------------------
109 echo UPGRADE OF POSTGRES COMPLETE
110 echo
111 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
112 echo
113 echo To complete the upgrade, rebuild again using:
114 echo
115 echo ./launcher rebuild app
116 echo -------------------------------------------------------------------------------------
117 # Magic exit status to denote no failure
118 exit 77
119 fi
120
121 - exec:
122 cmd:
123 - chown -R root /var/lib/postgresql/12/main
124 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/12/bin/initdb -D /shared/postgres_data || exit 0"
125 - chown -R postgres:postgres /shared/postgres_data
126 - chown -R postgres:postgres /var/run/postgresql
127
128 - exec: /root/upgrade_postgres
129 - exec: rm /root/upgrade_postgres
130
131 - replace:
132 filename: "/etc/postgresql/12/main/postgresql.conf"
133 from: "data_directory = '/var/lib/postgresql/12/main'"
134 to: "data_directory = '/shared/postgres_data'"
135
136 # listen on all interfaces
137 - replace:
138 filename: "/etc/postgresql/12/main/postgresql.conf"
139 from: /#?listen_addresses *=.*/
140 to: "listen_addresses = '*'"
141
142 # sync commit off is faster and less spiky, also marginally less safe
143 - replace:
144 filename: "/etc/postgresql/12/main/postgresql.conf"
145 from: /#?synchronous_commit *=.*/
146 to: "synchronous_commit = $db_synchronous_commit"
147
148 # default is 128MB which is way too small
149 - replace:
150 filename: "/etc/postgresql/12/main/postgresql.conf"
151 from: /#?shared_buffers *=.*/
152 to: "shared_buffers = $db_shared_buffers"
153
154 # default is 1MB which is too small
155 - replace:
156 filename: "/etc/postgresql/12/main/postgresql.conf"
157 from: /#?work_mem *=.*/
158 to: "work_mem = $db_work_mem"
159
160 # allow for other
161 - replace:
162 filename: "/etc/postgresql/12/main/postgresql.conf"
163 from: /#?default_text_search_config *=.*/
164 to: "default_text_search_config = '$db_default_text_search_config'"
165
166 # Necessary to enable backups
167 - exec:
168 cmd:
169 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
170
171 - replace:
172 filename: "/etc/postgresql/12/main/postgresql.conf"
173 from: /#?max_wal_senders *=.*/
174 to: "max_wal_senders = $db_max_wal_senders"
175
176 - replace:
177 filename: "/etc/postgresql/12/main/postgresql.conf"
178 from: /#?wal_level *=.*/
179 to: "wal_level = $db_wal_level"
180
181 - replace:
182 filename: "/etc/postgresql/12/main/postgresql.conf"
183 from: /#?checkpoint_segments *=.*/
184 to: "checkpoint_segments = $db_checkpoint_segments"
185
186 - replace:
187 filename: "/etc/postgresql/12/main/postgresql.conf"
188 from: /#?logging_collector *=.*/
189 to: "logging_collector = $db_logging_collector"
190
191 - replace:
192 filename: "/etc/postgresql/12/main/postgresql.conf"
193 from: /#?log_min_duration_statement *=.*/
194 to: "log_min_duration_statement = $db_log_min_duration_statement"
195
196 - replace:
197 filename: "/etc/postgresql/12/main/pg_hba.conf"
198 from: /^#local +replication +postgres +peer$/
199 to: "local replication postgres peer"
200
201 # allow all to connect in with md5 auth
202 - replace:
203 filename: "/etc/postgresql/12/main/pg_hba.conf"
204 from: /^host.*all.*all.*127.*$/
205 to: "host all all 0.0.0.0/0 md5"
206
207 - exec:
208 background: true
209 # use fast shutdown for pg
210 stop_signal: INT
211 cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/12/bin/postmaster -D /etc/postgresql/12/main
212
213 # give db a few secs to start up
214 - exec: "sleep 5"
215
216 - exec: su postgres -c 'createdb $db_name' || true
217 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
218 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
219 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
220 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
221 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
222 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
223 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
224 - exec:
225 stdin: |
226 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
227 cmd: sudo -u postgres psql $db_name
228 raise_on_fail: false
229
230 - file:
231 path: /var/lib/postgresql/take-database-backup
232 chown: postgres:postgres
233 chmod: "+x"
234 contents: |
235 #!/bin/bash
236 ID=db-$(date +%F_%T)
237 FILENAME=/shared/postgres_backup/$ID.tar.gz
238 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
239 echo $FILENAME
240
241 - file:
242 path: /var/spool/cron/crontabs/postgres
243 contents: |
244 # m h dom mon dow command
245 #MAILTO=?
246 #0 */4 * * * /var/lib/postgresql/take-database-backup
247
248 - exec:
249 hook: postgres
250 cmd: "echo postgres installed!"