2 db_synchronous_commit: "off"
3 db_shared_buffers: "256MB"
5 db_default_text_search_config: "pg_catalog.english"
10 db_checkpoint_segments: 6
11 db_logging_collector: off
12 db_log_min_duration_statement: 100
18 filename: /etc/service/unicorn/run
20 to: sv start postgres || exit 1
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
29 - exec: rm -fr /shared/postgres_run/.s*
30 - exec: rm -fr /shared/postgres_run/*.pid
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
34 path: /etc/service/postgres/run
39 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
41 path: /etc/service/postgres/log/run
45 mkdir -p /var/log/postgres
46 exec svlogd /var/log/postgres
49 path: /etc/runit/3.d/99-postgres
56 path: /root/upgrade_postgres
60 PG_MAJOR_OLD=`cat /shared/postgres_data/PG_VERSION`
62 if [ ! "9.5" = "$PG_MAJOR_OLD" ]; then
63 echo Upgrading PostgreSQL to from version ${PG_MAJOR_OLD} to 9.5
64 rm -fr /shared/postgres_data_new
65 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
67 apt-get install -y postgresql-${PG_MAJOR_OLD}
68 /etc/init.d/postgresql stop
69 rm -fr /shared/postgres_data/postmaster.pid
71 cp /etc/postgresql/${PG_MAJOR_OLD}/main/*.conf /shared/postgres_data
72 echo >> /shared/postgres_data/postgresql.conf
73 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
75 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
77 if [[ "$SUCCESS" == 'false' ]]; then
78 echo -------------------------------------------------------------------------------------
79 echo UPGRADE OF POSTGRES FAILED
81 echo You are going to need to export your data and import into a clean instance:
84 if [ "$PG_MAJOR_OLD" = "9.2" ]; then
85 echo Add the line: 'base_image: "samsaffron/discourse:0.1.1"'
86 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.2.template.yml"'
90 if [ "$PG_MAJOR_OLD" = "9.3" ]; then
91 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.3.template.yml"'
95 echo Run "./launcher rebuild app" again
97 echo When your instance is running:
98 echo Run "./launcher enter app"
99 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
101 if [ "$PG_MAJOR_OLD" = "9.2" ]; then
102 echo Undo the base_image in your container config
104 if [ "$PG_MAJOR_OLD" = "9.3" ]; then
105 echo Undo the postgres template in your container config
107 echo Run: "./launcher stop app"
108 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
109 echo Run: "./launcher rebuild app"
111 echo Run: "./launcher enter app"
112 echo Run: "cd /shared/postgres_backup"
113 echo Run: "sv stop unicorn"
114 echo Run: "sudo -iu postgres dropdb discourse"
115 echo Run: "sudo -iu postgres createdb discourse"
116 echo Run: "sudo -iu postgres psql discourse < backup.db"
118 echo Run: "./launcher rebuild app"
122 mv /shared/postgres_data /shared/postgres_data_old
123 mv /shared/postgres_data_new /shared/postgres_data
125 echo -------------------------------------------------------------------------------------
126 echo UPGRADE OF POSTGRES COMPLETE
128 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
130 echo To complete the upgrade, rebuild again using:
132 echo ./launcher rebuild app
133 echo -------------------------------------------------------------------------------------
134 # Magic exit status to denote no failure
139 - chown -R root /var/lib/postgresql/9.5/main
140 - "[ ! -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"
141 - chown -R postgres:postgres /shared/postgres_data
142 - chown -R postgres:postgres /var/run/postgresql
144 - exec: /root/upgrade_postgres
145 - exec: rm /root/upgrade_postgres
148 filename: "/etc/postgresql/9.5/main/postgresql.conf"
149 from: "data_directory = '/var/lib/postgresql/9.5/main'"
150 to: "data_directory = '/shared/postgres_data'"
152 # listen on all interfaces
154 filename: "/etc/postgresql/9.5/main/postgresql.conf"
155 from: /#?listen_addresses *=.*/
156 to: "listen_addresses = '*'"
158 # sync commit off is faster and less spiky, also marginally less safe
160 filename: "/etc/postgresql/9.5/main/postgresql.conf"
161 from: /#?synchronous_commit *=.*/
162 to: "synchronous_commit = $db_synchronous_commit"
164 # default is 128MB which is way too small
166 filename: "/etc/postgresql/9.5/main/postgresql.conf"
167 from: /#?shared_buffers *=.*/
168 to: "shared_buffers = $db_shared_buffers"
170 # default is 1MB which is too small
172 filename: "/etc/postgresql/9.5/main/postgresql.conf"
173 from: /#?work_mem *=.*/
174 to: "work_mem = $db_work_mem"
178 filename: "/etc/postgresql/9.5/main/postgresql.conf"
179 from: /#?default_text_search_config *=.*/
180 to: "default_text_search_config = '$db_default_text_search_config'"
182 # Necessary to enable backups
185 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
188 filename: "/etc/postgresql/9.5/main/postgresql.conf"
189 from: /#?max_wal_senders *=.*/
190 to: "max_wal_senders = $db_max_wal_senders"
193 filename: "/etc/postgresql/9.5/main/postgresql.conf"
194 from: /#?wal_level *=.*/
195 to: "wal_level = $db_wal_level"
198 filename: "/etc/postgresql/9.5/main/postgresql.conf"
199 from: /#?checkpoint_segments *=.*/
200 to: "checkpoint_segments = $db_checkpoint_segments"
203 filename: "/etc/postgresql/9.5/main/postgresql.conf"
204 from: /#?logging_collector *=.*/
205 to: "logging_collector = $db_logging_collector"
208 filename: "/etc/postgresql/9.5/main/postgresql.conf"
209 from: /#?log_min_duration_statement *=.*/
210 to: "log_min_duration_statement = $db_log_min_duration_statement"
213 filename: "/etc/postgresql/9.5/main/pg_hba.conf"
214 from: /^#local +replication +postgres +peer$/
215 to: "local replication postgres peer"
217 # allow all to connect in with md5 auth
219 filename: "/etc/postgresql/9.5/main/pg_hba.conf"
220 from: /^host.*all.*all.*127.*$/
221 to: "host all all 0.0.0.0/0 md5"
225 # use fast shutdown for pg
227 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
229 # give db a few secs to start up
232 - exec: su postgres -c 'createdb $db_name' || true
233 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
234 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
235 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
236 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
237 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
238 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
239 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
242 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
243 cmd: sudo -u postgres psql $db_name
247 path: /var/lib/postgresql/take-database-backup
248 chown: postgres:postgres
253 FILENAME=/shared/postgres_backup/$ID.tar.gz
254 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
258 path: /var/spool/cron/crontabs/postgres
260 # m h dom mon dow command
262 #0 */4 * * * /var/lib/postgresql/take-database-backup
266 cmd: "echo postgres installed!"