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