Commit | Line | Data |
---|---|---|
6f99f3c2 S |
1 | params: |
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 | 14 | hooks: |
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 | 21 | run: |
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 | ||
b20ba9ee RSS |
76 | if [ -d /shared/postgres_data_old ]; then |
77 | mv /shared/postgres_data_old /shared/postgres_data_older | |
78 | fi | |
79 | ||
d69fe0fb | 80 | rm -fr /shared/postgres_data_new |
4cdd4c8a | 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 |
a0958e4f | 82 | apt-get update |
49b852c5 | 83 | apt-get install -y postgresql-${PG_MAJOR_OLD} |
4cdd4c8a | 84 | chown -R postgres:postgres /var/lib/postgresql/12 |
61975bbd SS |
85 | /etc/init.d/postgresql stop |
86 | rm -fr /shared/postgres_data/postmaster.pid | |
d69fe0fb | 87 | cd ~postgres |
8194e684 | 88 | cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data |
61975bbd SS |
89 | echo >> /shared/postgres_data/postgresql.conf |
90 | echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf | |
9b9cdd3b | 91 | SUCCESS=true |
4cdd4c8a | 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 |
9b9cdd3b S |
93 | |
94 | if [[ "$SUCCESS" == 'false' ]]; then | |
95 | echo ------------------------------------------------------------------------------------- | |
96 | echo UPGRADE OF POSTGRES FAILED | |
97 | echo | |
416fe173 | 98 | echo Please visit https://meta.discourse.org/t/postgresql-12-update/151236 for support |
9b9cdd3b | 99 | echo |
416fe173 | 100 | echo You can run "./launcher start app" to restart your app in the meanwhile |
9b9cdd3b | 101 | echo |
9b9cdd3b S |
102 | exit 1 |
103 | fi | |
104 | ||
d69fe0fb S |
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 | |
49b852c5 | 111 | echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old |
d69fe0fb | 112 | echo |
72b89d4d AH |
113 | echo To complete the upgrade, rebuild again using: |
114 | echo | |
115 | echo ./launcher rebuild app | |
d69fe0fb | 116 | echo ------------------------------------------------------------------------------------- |
b4ab14c2 S |
117 | # Magic exit status to denote no failure |
118 | exit 77 | |
d69fe0fb | 119 | fi |
8194e684 | 120 | |
9fb5f2d3 SS |
121 | - exec: |
122 | cmd: | |
4cdd4c8a RSS |
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" | |
9fb5f2d3 | 125 | - chown -R postgres:postgres /shared/postgres_data |
d69fe0fb | 126 | - chown -R postgres:postgres /var/run/postgresql |
9fb5f2d3 | 127 | |
61975bbd | 128 | - exec: /root/upgrade_postgres |
6f99f3c2 S |
129 | - exec: rm /root/upgrade_postgres |
130 | ||
9fb5f2d3 | 131 | - replace: |
4cdd4c8a RSS |
132 | filename: "/etc/postgresql/12/main/postgresql.conf" |
133 | from: "data_directory = '/var/lib/postgresql/12/main'" | |
9fb5f2d3 SS |
134 | to: "data_directory = '/shared/postgres_data'" |
135 | ||
80c11be3 SS |
136 | # listen on all interfaces |
137 | - replace: | |
4cdd4c8a | 138 | filename: "/etc/postgresql/12/main/postgresql.conf" |
80c11be3 SS |
139 | from: /#?listen_addresses *=.*/ |
140 | to: "listen_addresses = '*'" | |
141 | ||
6f99f3c2 S |
142 | # sync commit off is faster and less spiky, also marginally less safe |
143 | - replace: | |
4cdd4c8a | 144 | filename: "/etc/postgresql/12/main/postgresql.conf" |
6f99f3c2 S |
145 | from: /#?synchronous_commit *=.*/ |
146 | to: "synchronous_commit = $db_synchronous_commit" | |
147 | ||
148 | # default is 128MB which is way too small | |
149 | - replace: | |
4cdd4c8a | 150 | filename: "/etc/postgresql/12/main/postgresql.conf" |
6f99f3c2 S |
151 | from: /#?shared_buffers *=.*/ |
152 | to: "shared_buffers = $db_shared_buffers" | |
153 | ||
154 | # default is 1MB which is too small | |
155 | - replace: | |
4cdd4c8a | 156 | filename: "/etc/postgresql/12/main/postgresql.conf" |
6f99f3c2 S |
157 | from: /#?work_mem *=.*/ |
158 | to: "work_mem = $db_work_mem" | |
159 | ||
160 | # allow for other | |
161 | - replace: | |
4cdd4c8a | 162 | filename: "/etc/postgresql/12/main/postgresql.conf" |
6f99f3c2 S |
163 | from: /#?default_text_search_config *=.*/ |
164 | to: "default_text_search_config = '$db_default_text_search_config'" | |
165 | ||
9fb5f2d3 SS |
166 | # Necessary to enable backups |
167 | - exec: | |
168 | cmd: | |
169 | - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup | |
170 | ||
171 | - replace: | |
4cdd4c8a | 172 | filename: "/etc/postgresql/12/main/postgresql.conf" |
9fb5f2d3 | 173 | from: /#?max_wal_senders *=.*/ |
83d3ef11 | 174 | to: "max_wal_senders = $db_max_wal_senders" |
9fb5f2d3 SS |
175 | |
176 | - replace: | |
4cdd4c8a | 177 | filename: "/etc/postgresql/12/main/postgresql.conf" |
9fb5f2d3 | 178 | from: /#?wal_level *=.*/ |
83d3ef11 S |
179 | to: "wal_level = $db_wal_level" |
180 | ||
181 | - replace: | |
4cdd4c8a | 182 | filename: "/etc/postgresql/12/main/postgresql.conf" |
83d3ef11 S |
183 | from: /#?checkpoint_segments *=.*/ |
184 | to: "checkpoint_segments = $db_checkpoint_segments" | |
185 | ||
186 | - replace: | |
4cdd4c8a | 187 | filename: "/etc/postgresql/12/main/postgresql.conf" |
83d3ef11 S |
188 | from: /#?logging_collector *=.*/ |
189 | to: "logging_collector = $db_logging_collector" | |
190 | ||
191 | - replace: | |
4cdd4c8a | 192 | filename: "/etc/postgresql/12/main/postgresql.conf" |
83d3ef11 S |
193 | from: /#?log_min_duration_statement *=.*/ |
194 | to: "log_min_duration_statement = $db_log_min_duration_statement" | |
9fb5f2d3 SS |
195 | |
196 | - replace: | |
4cdd4c8a | 197 | filename: "/etc/postgresql/12/main/pg_hba.conf" |
9fb5f2d3 SS |
198 | from: /^#local +replication +postgres +peer$/ |
199 | to: "local replication postgres peer" | |
80c11be3 SS |
200 | |
201 | # allow all to connect in with md5 auth | |
202 | - replace: | |
4cdd4c8a | 203 | filename: "/etc/postgresql/12/main/pg_hba.conf" |
80c11be3 SS |
204 | from: /^host.*all.*all.*127.*$/ |
205 | to: "host all all 0.0.0.0/0 md5" | |
206 | ||
207 | - exec: | |
208 | background: true | |
c244475c S |
209 | # use fast shutdown for pg |
210 | stop_signal: INT | |
4cdd4c8a | 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 |
80c11be3 SS |
212 | |
213 | # give db a few secs to start up | |
214 | - exec: "sleep 5" | |
215 | ||
cfd0322a GXT |
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;"' | |
7f4eef4c | 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;"' | |
cfd0322a GXT |
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;"' | |
2b09a847 AT |
224 | - exec: |
225 | stdin: | | |
e4eb2bc0 | 226 | update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII'); |
cfd0322a | 227 | cmd: sudo -u postgres psql $db_name |
2b09a847 | 228 | raise_on_fail: false |
80c11be3 | 229 | |
be82e068 S |
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=? | |
e56a65f6 | 246 | #0 */4 * * * /var/lib/postgresql/take-database-backup |
be82e068 | 247 | |
80c11be3 SS |
248 | - exec: |
249 | hook: postgres | |
250 | cmd: "echo postgres installed!" |