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