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