FEATURE: allow tweaking more pg setup options
[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"
5 db_default_text_search_config: "pb_catelog.english"
6
9fb5f2d3
SS
7hooks:
8 after_cron:
9 - file:
10 path: /var/lib/postgresql/take-database-backup
11 chown: postgres:postgres
12 chmod: "+x"
13 contents: |
14 #!/bin/bash
15 ID=db-$(date +%F_%T)
16 FILENAME=/shared/postgres_backup/$ID.tar.gz
17 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
18 echo $FILENAME
19
20 - file:
21 path: /var/spool/cron/crontabs/postgres
22 contents: |
23 # m h dom mon dow command
24 #MAILTO=?
25 0 */4 * * * /var/lib/postgresql/take-database-backup
26
27 before_code:
9fb5f2d3
SS
28 - replace:
29 filename: /etc/service/unicorn/run
30 from: "# postgres"
31 to: sv start postgres || exit 1
32
9fb5f2d3 33run:
d7ed7814
SS
34 # temporary
35 - exec: apt-get install -y socat
36 - exec: mkdir -p /shared/postgres_run
37 - exec: chown postgres:postgres /shared/postgres_run
38 - exec: chmod 775 /shared/postgres_run
39 - exec: rm -fr /var/run/postgresql
40 - exec: ln -s /shared/postgres_run /var/run/postgresql
41 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
61975bbd
SS
42 - exec: rm -fr /shared/postgres_run/.s*
43 - exec: rm -fr /shared/postgres_run/*.pid
9fb5f2d3
SS
44 - file:
45 path: /etc/service/postgres/run
46 chmod: "+x"
47 contents: |
48 #!/bin/sh
49 exec 2>&1
d69fe0fb
S
50 exec sudo -u postgres /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main
51 - file:
52 path: /root/upgrade_postgres
53 chmod: "+x"
54 contents: |
55 #!/bin/bash
56 if [ "9.2" = `cat /shared/postgres_data/PG_VERSION` ]; then
57 echo Upgrading PostgreSQL to from version 9.2 to 9.3
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.3/bin/initdb -D /shared/postgres_data_new || exit 0
a0958e4f 60 apt-get update
d69fe0fb 61 apt-get install -y postgresql-9.2
61975bbd
SS
62 /etc/init.d/postgresql stop
63 rm -fr /shared/postgres_data/postmaster.pid
d69fe0fb 64 cd ~postgres
61975bbd
SS
65 cp /etc/postgresql/9.2/main/*.conf /shared/postgres_data
66 echo >> /shared/postgres_data/postgresql.conf
67 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
9b9cdd3b
S
68 SUCCESS=true
69 sudo -u postgres /usr/lib/postgresql/9.3/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/9.2/bin -B /usr/lib/postgresql/9.3/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 echo Add the line: 'base_image: "samsaffron/discourse:0.1.1"'
78 echo Change: "templates/postgres.template.yml TO templates/postgres.9.2.template.yml"
79 echo
80 echo Run "./launcher bootstrap" again
81 echo
82 echo When your instance is running:
83 echo - Go to the admin UI, enable readonly and backup your site
84 echo
85 echo Then destroy your container "./launcher destroy standalone"
86 echo Undo the base_image and postgres.9.2 template from your container config
87 echo Run: "sudo mv /var/docker/shared/postgres_data /var/shared/postgres_data_old"
88 echo Run: "./launcher bootstrap"
89 echo
62418f96 90 echo "Login to the blank site, and import your backup (remember to set the allow_restore site setting)"
9b9cdd3b
S
91 echo Restart your container: "./launcher restart"
92 echo -------------------------------------------------------------------------------------
93 exit 1
94 fi
95
d69fe0fb
S
96 mv /shared/postgres_data /shared/postgres_data_old
97 mv /shared/postgres_data_new /shared/postgres_data
98
99 echo -------------------------------------------------------------------------------------
100 echo UPGRADE OF POSTGRES COMPLETE
101 echo
102 echo Old 9.2 database is stored at /shared/postgres_data_old
103 echo
104 echo Please run "./launcher bootstrap" again
105 echo -------------------------------------------------------------------------------------
106 exit 1
107 fi
9fb5f2d3
SS
108 - exec:
109 cmd:
d69fe0fb 110 - chown -R root /var/lib/postgresql/9.3/main
3cb3d9c4 111 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/9.3/bin/initdb -D /shared/postgres_data || exit 0"
9fb5f2d3 112 - chown -R postgres:postgres /shared/postgres_data
d69fe0fb 113 - chown -R postgres:postgres /var/run/postgresql
9fb5f2d3 114
61975bbd 115 - exec: /root/upgrade_postgres
6f99f3c2
S
116 - exec: rm /root/upgrade_postgres
117
9fb5f2d3 118 - replace:
d69fe0fb
S
119 filename: "/etc/postgresql/9.3/main/postgresql.conf"
120 from: "data_directory = '/var/lib/postgresql/9.3/main'"
9fb5f2d3
SS
121 to: "data_directory = '/shared/postgres_data'"
122
80c11be3
SS
123 # listen on all interfaces
124 - replace:
d69fe0fb 125 filename: "/etc/postgresql/9.3/main/postgresql.conf"
80c11be3
SS
126 from: /#?listen_addresses *=.*/
127 to: "listen_addresses = '*'"
128
6f99f3c2
S
129 # listen on all interfaces
130 - replace:
131 filename: "/etc/postgresql/9.3/main/postgresql.conf"
132 from: /#?listen_addresses *=.*/
133 to: "listen_addresses = '*'"
134
135 # sync commit off is faster and less spiky, also marginally less safe
136 - replace:
137 filename: "/etc/postgresql/9.3/main/postgresql.conf"
138 from: /#?synchronous_commit *=.*/
139 to: "synchronous_commit = $db_synchronous_commit"
140
141 # default is 128MB which is way too small
142 - replace:
143 filename: "/etc/postgresql/9.3/main/postgresql.conf"
144 from: /#?shared_buffers *=.*/
145 to: "shared_buffers = $db_shared_buffers"
146
147 # default is 1MB which is too small
148 - replace:
149 filename: "/etc/postgresql/9.3/main/postgresql.conf"
150 from: /#?work_mem *=.*/
151 to: "work_mem = $db_work_mem"
152
153 # allow for other
154 - replace:
155 filename: "/etc/postgresql/9.3/main/postgresql.conf"
156 from: /#?default_text_search_config *=.*/
157 to: "default_text_search_config = '$db_default_text_search_config'"
158
9fb5f2d3
SS
159 # Necessary to enable backups
160 - exec:
161 cmd:
162 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
163
164 - replace:
d69fe0fb 165 filename: "/etc/postgresql/9.3/main/postgresql.conf"
9fb5f2d3
SS
166 from: /#?max_wal_senders *=.*/
167 to: "max_wal_senders = 4"
168
169 - replace:
d69fe0fb 170 filename: "/etc/postgresql/9.3/main/postgresql.conf"
9fb5f2d3
SS
171 from: /#?wal_level *=.*/
172 to: "wal_level = hot_standby"
173
174 - replace:
d69fe0fb 175 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
9fb5f2d3
SS
176 from: /^#local +replication +postgres +peer$/
177 to: "local replication postgres peer"
80c11be3
SS
178
179 # allow all to connect in with md5 auth
180 - replace:
d69fe0fb 181 filename: "/etc/postgresql/9.3/main/pg_hba.conf"
80c11be3
SS
182 from: /^host.*all.*all.*127.*$/
183 to: "host all all 0.0.0.0/0 md5"
184
185 - exec:
186 background: true
d69fe0fb 187 cmd: "sudo -u postgres /usr/lib/postgresql/9.3/bin/postmaster -D /etc/postgresql/9.3/main"
80c11be3
SS
188
189 # give db a few secs to start up
190 - exec: "sleep 5"
191
192 - exec: sudo -u postgres createdb discourse || exit 0
193 - exec:
194 stdin: |
195 create user discourse;
196 cmd: sudo -u postgres psql discourse
197 raise_on_fail: false
198
199 - exec:
200 stdin: |
201 grant all privileges on database discourse to discourse;
202 cmd: sudo -u postgres psql discourse
203 raise_on_fail: false
204
69d4c4c3 205 - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "alter schema public owner to discourse;"'
80c11be3
SS
206 - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists hstore;"'
207 - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists pg_trgm;"'
208
209 - exec:
210 hook: postgres
211 cmd: "echo postgres installed!"