Merge pull request #267 from watchmanmonitoring/yesthatallen-clarify-steps
[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 echo -1000 >/proc/self/oom_score_adj
40 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
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 [ ! "9.5" = "$PG_MAJOR_OLD" ]; then
57 echo Upgrading PostgreSQL to from version ${PG_MAJOR_OLD} to 9.5
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/9.5/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 /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/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
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.2" ]; then
79 echo Add the line: 'base_image: "samsaffron/discourse:0.1.1"'
80 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.2.template.yml"'
81 echo
82 fi
83
84 if [ "$PG_MAJOR_OLD" = "9.3" ]; then
85 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.3.template.yml"'
86 echo
87 fi
88
89 echo Run "./launcher rebuild app" again
90 echo
91 echo When your instance is running:
92 echo Run "./launcher enter app"
93 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
94 echo
95 echo Undo the base_image and postgres template from your container config
96 echo Run: "./launcher stop app"
97 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
98 echo Run: "./launcher rebuild app"
99 echo
100 echo Run: "./launcher enter app"
101 echo Run: "cd /shared/postgres_backup"
102 echo Run: "sv stop unicorn"
103 echo Run: "sudo -iu postgres dropdb discourse"
104 echo Run: "sudo -iu postgres createdb discourse"
105 echo Run: "sudo -iu postgres psql discourse < backup.db"
106 echo Run: "exit"
107 echo Run: "./launcher rebuild app"
108 exit 1
109 fi
110
111 mv /shared/postgres_data /shared/postgres_data_old
112 mv /shared/postgres_data_new /shared/postgres_data
113
114 echo -------------------------------------------------------------------------------------
115 echo UPGRADE OF POSTGRES COMPLETE
116 echo
117 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
118 echo
119 echo To complete the upgrade, rebuild again using:
120 echo
121 echo ./launcher rebuild app
122 echo -------------------------------------------------------------------------------------
123 # Magic exit status to denote no failure
124 exit 77
125 fi
126 - exec:
127 cmd:
128 - chown -R root /var/lib/postgresql/9.5/main
129 - "[ ! -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"
130 - chown -R postgres:postgres /shared/postgres_data
131 - chown -R postgres:postgres /var/run/postgresql
132
133 - exec: /root/upgrade_postgres
134 - exec: rm /root/upgrade_postgres
135
136 - replace:
137 filename: "/etc/postgresql/9.5/main/postgresql.conf"
138 from: "data_directory = '/var/lib/postgresql/9.5/main'"
139 to: "data_directory = '/shared/postgres_data'"
140
141 # listen on all interfaces
142 - replace:
143 filename: "/etc/postgresql/9.5/main/postgresql.conf"
144 from: /#?listen_addresses *=.*/
145 to: "listen_addresses = '*'"
146
147 # sync commit off is faster and less spiky, also marginally less safe
148 - replace:
149 filename: "/etc/postgresql/9.5/main/postgresql.conf"
150 from: /#?synchronous_commit *=.*/
151 to: "synchronous_commit = $db_synchronous_commit"
152
153 # default is 128MB which is way too small
154 - replace:
155 filename: "/etc/postgresql/9.5/main/postgresql.conf"
156 from: /#?shared_buffers *=.*/
157 to: "shared_buffers = $db_shared_buffers"
158
159 # default is 1MB which is too small
160 - replace:
161 filename: "/etc/postgresql/9.5/main/postgresql.conf"
162 from: /#?work_mem *=.*/
163 to: "work_mem = $db_work_mem"
164
165 # allow for other
166 - replace:
167 filename: "/etc/postgresql/9.5/main/postgresql.conf"
168 from: /#?default_text_search_config *=.*/
169 to: "default_text_search_config = '$db_default_text_search_config'"
170
171 # Necessary to enable backups
172 - exec:
173 cmd:
174 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
175
176 - replace:
177 filename: "/etc/postgresql/9.5/main/postgresql.conf"
178 from: /#?max_wal_senders *=.*/
179 to: "max_wal_senders = $db_max_wal_senders"
180
181 - replace:
182 filename: "/etc/postgresql/9.5/main/postgresql.conf"
183 from: /#?wal_level *=.*/
184 to: "wal_level = $db_wal_level"
185
186 - replace:
187 filename: "/etc/postgresql/9.5/main/postgresql.conf"
188 from: /#?checkpoint_segments *=.*/
189 to: "checkpoint_segments = $db_checkpoint_segments"
190
191 - replace:
192 filename: "/etc/postgresql/9.5/main/postgresql.conf"
193 from: /#?logging_collector *=.*/
194 to: "logging_collector = $db_logging_collector"
195
196 - replace:
197 filename: "/etc/postgresql/9.5/main/postgresql.conf"
198 from: /#?log_min_duration_statement *=.*/
199 to: "log_min_duration_statement = $db_log_min_duration_statement"
200
201 - replace:
202 filename: "/etc/postgresql/9.5/main/pg_hba.conf"
203 from: /^#local +replication +postgres +peer$/
204 to: "local replication postgres peer"
205
206 # allow all to connect in with md5 auth
207 - replace:
208 filename: "/etc/postgresql/9.5/main/pg_hba.conf"
209 from: /^host.*all.*all.*127.*$/
210 to: "host all all 0.0.0.0/0 md5"
211
212 - exec:
213 background: true
214 # use fast shutdown for pg
215 stop_signal: INT
216 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
217
218 # give db a few secs to start up
219 - exec: "sleep 5"
220
221 - exec: su postgres -c 'createdb $db_name' || true
222 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
223 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
224 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
225 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
226 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
227 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
228 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
229 - exec:
230 stdin: |
231 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
232 cmd: sudo -u postgres psql $db_name
233 raise_on_fail: false
234
235 - file:
236 path: /var/lib/postgresql/take-database-backup
237 chown: postgres:postgres
238 chmod: "+x"
239 contents: |
240 #!/bin/bash
241 ID=db-$(date +%F_%T)
242 FILENAME=/shared/postgres_backup/$ID.tar.gz
243 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
244 echo $FILENAME
245
246 - file:
247 path: /var/spool/cron/crontabs/postgres
248 contents: |
249 # m h dom mon dow command
250 #MAILTO=?
251 #0 */4 * * * /var/lib/postgresql/take-database-backup
252
253 - exec:
254 hook: postgres
255 cmd: "echo postgres installed!"