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