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