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