Simpler instruction in case of pg_upgrade failure
[discourse_docker.git] / templates / postgres.template.yml
CommitLineData
6f99f3c2
S
1params:
2 db_synchronous_commit: "off"
3 db_shared_buffers: "256MB"
4 db_work_mem: "10MB"
8019560c 5 db_default_text_search_config: "pg_catalog.english"
cfd0322a
GXT
6 db_name: discourse
7 db_user: discourse
83d3ef11
S
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
9fb5f2d3 14hooks:
9fb5f2d3 15 before_code:
9fb5f2d3
SS
16 - replace:
17 filename: /etc/service/unicorn/run
18 from: "# postgres"
19 to: sv start postgres || exit 1
20
9fb5f2d3 21run:
d4eb8c3f 22 - exec: locale-gen $LANG && update-locale
d7ed7814
SS
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
61975bbd
SS
29 - exec: rm -fr /shared/postgres_run/.s*
30 - exec: rm -fr /shared/postgres_run/*.pid
4cdd4c8a
RSS
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
9fb5f2d3
SS
33 - file:
34 path: /etc/service/postgres/run
35 chmod: "+x"
36 contents: |
37 #!/bin/sh
38 exec 2>&1
4cdd4c8a 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
8194e684 40
3f9f3c3b
S
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
074f2b6a
S
48
49 - file:
50 path: /etc/runit/3.d/99-postgres
51 chmod: "+x"
52 contents: |
53 #!/bin/bash
54 sv stop postgres
55
d69fe0fb
S
56 - file:
57 path: /root/upgrade_postgres
58 chmod: "+x"
59 contents: |
60 #!/bin/bash
49b852c5
S
61 PG_MAJOR_OLD=`cat /shared/postgres_data/PG_VERSION`
62
4cdd4c8a
RSS
63 if [ ! "12" = "$PG_MAJOR_OLD" ]; then
64 echo Upgrading PostgreSQL from version ${PG_MAJOR_OLD} to 12
a92a9257 65 free_disk=$(df -P -B1 /shared | tail -n 1 | awk '{print $4}')
882358d5 66 required=$(($(du -sb /shared/postgres_data | awk '{print $1}') * 2))
448a70b9
GXT
67
68 if [ "$free_disk" -lt "$required" ]; then
969cc9b9 69 echo "WARNING: Upgrading PostgresSQL would require an addtional $(numfmt --to=si $(($required - $free_disk))) of disk space"
448a70b9 70 echo "Please free up some space, or expand your disk, before continuing."
5e02f7e4 71 echo ''
4cdd4c8a 72 echo 'To avoid upgrading change "templates/postgres.template.yml" TO "templates/postgres.10.template.yml" in containers/app.yml'
448a70b9
GXT
73 exit 1
74 fi
75
d69fe0fb 76 rm -fr /shared/postgres_data_new
4cdd4c8a 77 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
a0958e4f 78 apt-get update
49b852c5 79 apt-get install -y postgresql-${PG_MAJOR_OLD}
4cdd4c8a 80 chown -R postgres:postgres /var/lib/postgresql/12
61975bbd
SS
81 /etc/init.d/postgresql stop
82 rm -fr /shared/postgres_data/postmaster.pid
d69fe0fb 83 cd ~postgres
8194e684 84 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
61975bbd
SS
85 echo >> /shared/postgres_data/postgresql.conf
86 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
9b9cdd3b 87 SUCCESS=true
4cdd4c8a 88 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
9b9cdd3b
S
89
90 if [[ "$SUCCESS" == 'false' ]]; then
91 echo -------------------------------------------------------------------------------------
92 echo UPGRADE OF POSTGRES FAILED
93 echo
416fe173 94 echo Please visit https://meta.discourse.org/t/postgresql-12-update/151236 for support
9b9cdd3b 95 echo
416fe173 96 echo You can run "./launcher start app" to restart your app in the meanwhile
9b9cdd3b 97 echo
9b9cdd3b
S
98 exit 1
99 fi
100
d69fe0fb
S
101 mv /shared/postgres_data /shared/postgres_data_old
102 mv /shared/postgres_data_new /shared/postgres_data
103
104 echo -------------------------------------------------------------------------------------
105 echo UPGRADE OF POSTGRES COMPLETE
106 echo
49b852c5 107 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
d69fe0fb 108 echo
72b89d4d
AH
109 echo To complete the upgrade, rebuild again using:
110 echo
111 echo ./launcher rebuild app
d69fe0fb 112 echo -------------------------------------------------------------------------------------
b4ab14c2
S
113 # Magic exit status to denote no failure
114 exit 77
d69fe0fb 115 fi
8194e684 116
9fb5f2d3
SS
117 - exec:
118 cmd:
4cdd4c8a
RSS
119 - chown -R root /var/lib/postgresql/12/main
120 - "[ ! -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"
9fb5f2d3 121 - chown -R postgres:postgres /shared/postgres_data
d69fe0fb 122 - chown -R postgres:postgres /var/run/postgresql
9fb5f2d3 123
61975bbd 124 - exec: /root/upgrade_postgres
6f99f3c2
S
125 - exec: rm /root/upgrade_postgres
126
9fb5f2d3 127 - replace:
4cdd4c8a
RSS
128 filename: "/etc/postgresql/12/main/postgresql.conf"
129 from: "data_directory = '/var/lib/postgresql/12/main'"
9fb5f2d3
SS
130 to: "data_directory = '/shared/postgres_data'"
131
80c11be3
SS
132 # listen on all interfaces
133 - replace:
4cdd4c8a 134 filename: "/etc/postgresql/12/main/postgresql.conf"
80c11be3
SS
135 from: /#?listen_addresses *=.*/
136 to: "listen_addresses = '*'"
137
6f99f3c2
S
138 # sync commit off is faster and less spiky, also marginally less safe
139 - replace:
4cdd4c8a 140 filename: "/etc/postgresql/12/main/postgresql.conf"
6f99f3c2
S
141 from: /#?synchronous_commit *=.*/
142 to: "synchronous_commit = $db_synchronous_commit"
143
144 # default is 128MB which is way too small
145 - replace:
4cdd4c8a 146 filename: "/etc/postgresql/12/main/postgresql.conf"
6f99f3c2
S
147 from: /#?shared_buffers *=.*/
148 to: "shared_buffers = $db_shared_buffers"
149
150 # default is 1MB which is too small
151 - replace:
4cdd4c8a 152 filename: "/etc/postgresql/12/main/postgresql.conf"
6f99f3c2
S
153 from: /#?work_mem *=.*/
154 to: "work_mem = $db_work_mem"
155
156 # allow for other
157 - replace:
4cdd4c8a 158 filename: "/etc/postgresql/12/main/postgresql.conf"
6f99f3c2
S
159 from: /#?default_text_search_config *=.*/
160 to: "default_text_search_config = '$db_default_text_search_config'"
161
9fb5f2d3
SS
162 # Necessary to enable backups
163 - exec:
164 cmd:
165 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
166
167 - replace:
4cdd4c8a 168 filename: "/etc/postgresql/12/main/postgresql.conf"
9fb5f2d3 169 from: /#?max_wal_senders *=.*/
83d3ef11 170 to: "max_wal_senders = $db_max_wal_senders"
9fb5f2d3
SS
171
172 - replace:
4cdd4c8a 173 filename: "/etc/postgresql/12/main/postgresql.conf"
9fb5f2d3 174 from: /#?wal_level *=.*/
83d3ef11
S
175 to: "wal_level = $db_wal_level"
176
177 - replace:
4cdd4c8a 178 filename: "/etc/postgresql/12/main/postgresql.conf"
83d3ef11
S
179 from: /#?checkpoint_segments *=.*/
180 to: "checkpoint_segments = $db_checkpoint_segments"
181
182 - replace:
4cdd4c8a 183 filename: "/etc/postgresql/12/main/postgresql.conf"
83d3ef11
S
184 from: /#?logging_collector *=.*/
185 to: "logging_collector = $db_logging_collector"
186
187 - replace:
4cdd4c8a 188 filename: "/etc/postgresql/12/main/postgresql.conf"
83d3ef11
S
189 from: /#?log_min_duration_statement *=.*/
190 to: "log_min_duration_statement = $db_log_min_duration_statement"
9fb5f2d3
SS
191
192 - replace:
4cdd4c8a 193 filename: "/etc/postgresql/12/main/pg_hba.conf"
9fb5f2d3
SS
194 from: /^#local +replication +postgres +peer$/
195 to: "local replication postgres peer"
80c11be3
SS
196
197 # allow all to connect in with md5 auth
198 - replace:
4cdd4c8a 199 filename: "/etc/postgresql/12/main/pg_hba.conf"
80c11be3
SS
200 from: /^host.*all.*all.*127.*$/
201 to: "host all all 0.0.0.0/0 md5"
202
203 - exec:
204 background: true
c244475c
S
205 # use fast shutdown for pg
206 stop_signal: INT
4cdd4c8a 207 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
80c11be3
SS
208
209 # give db a few secs to start up
210 - exec: "sleep 5"
211
cfd0322a
GXT
212 - exec: su postgres -c 'createdb $db_name' || true
213 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
214 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
215 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
7f4eef4c 216 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
217 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
cfd0322a
GXT
218 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
219 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
2b09a847
AT
220 - exec:
221 stdin: |
e4eb2bc0 222 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
cfd0322a 223 cmd: sudo -u postgres psql $db_name
2b09a847 224 raise_on_fail: false
80c11be3 225
be82e068
S
226 - file:
227 path: /var/lib/postgresql/take-database-backup
228 chown: postgres:postgres
229 chmod: "+x"
230 contents: |
231 #!/bin/bash
232 ID=db-$(date +%F_%T)
233 FILENAME=/shared/postgres_backup/$ID.tar.gz
234 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
235 echo $FILENAME
236
237 - file:
238 path: /var/spool/cron/crontabs/postgres
239 contents: |
240 # m h dom mon dow command
241 #MAILTO=?
e56a65f6 242 #0 */4 * * * /var/lib/postgresql/take-database-backup
be82e068 243
80c11be3
SS
244 - exec:
245 hook: postgres
246 cmd: "echo postgres installed!"