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