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
17 filename: /etc/service/unicorn/run
19 to: sv start postgres || exit 1
22 - exec: mkdir -p /shared/postgres_run
23 - exec: chown postgres:postgres /shared/postgres_run
24 - exec: chmod 775 /shared/postgres_run
25 - exec: rm -fr /var/run/postgresql
26 - exec: ln -s /shared/postgres_run /var/run/postgresql
27 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
28 - exec: rm -fr /shared/postgres_run/.s*
29 - exec: rm -fr /shared/postgres_run/*.pid
30 - exec: mkdir -p /shared/postgres_run/10-main.pg_stat_tmp
31 - exec: chown postgres:postgres /shared/postgres_run/10-main.pg_stat_tmp
33 path: /etc/service/postgres/run
38 HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main
41 path: /etc/service/postgres/log/run
45 mkdir -p /var/log/postgres
46 exec svlogd /var/log/postgres
50 path: /etc/runit/3.d/99-postgres
57 path: /root/upgrade_postgres
61 PG_MAJOR_OLD=`cat /shared/postgres_data/PG_VERSION`
63 if [ ! "10" = "$PG_MAJOR_OLD" ]; then
64 echo Upgrading PostgreSQL to from version ${PG_MAJOR_OLD} to 10
65 rm -fr /shared/postgres_data_new
66 install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/10/bin/initdb -D /shared/postgres_data_new || exit 0
68 apt-get install -y postgresql-${PG_MAJOR_OLD}
69 chown -R postgres:postgres /var/lib/postgresql/10
70 /etc/init.d/postgresql stop
71 rm -fr /shared/postgres_data/postmaster.pid
73 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
74 echo >> /shared/postgres_data/postgresql.conf
75 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
77 sudo -u postgres /usr/lib/postgresql/10/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/${PG_MAJOR_OLD}/bin -B /usr/lib/postgresql/10/bin || SUCCESS=false
79 if [[ "$SUCCESS" == 'false' ]]; then
80 echo -------------------------------------------------------------------------------------
81 echo UPGRADE OF POSTGRES FAILED
83 echo You are going to need to export your data and import into a clean instance:
86 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
87 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml"'
91 echo Run "./launcher rebuild app" again
93 echo When your instance is running:
94 echo Run "./launcher enter app"
95 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
98 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
99 echo Undo the base_image in your container config
102 echo Run: "./launcher stop app"
103 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
104 echo Run: "./launcher rebuild app"
106 echo Run: "./launcher enter app"
107 echo Run: "cd /shared/postgres_backup"
108 echo Run: "sv stop unicorn"
109 echo Run: "sudo -iu postgres dropdb discourse"
110 echo Run: "sudo -iu postgres createdb discourse"
111 echo Run: "sudo -iu postgres psql discourse < backup.db"
113 echo Run: "./launcher rebuild app"
117 mv /shared/postgres_data /shared/postgres_data_old
118 mv /shared/postgres_data_new /shared/postgres_data
120 echo -------------------------------------------------------------------------------------
121 echo UPGRADE OF POSTGRES COMPLETE
123 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
125 echo To complete the upgrade, rebuild again using:
127 echo ./launcher rebuild app
128 echo -------------------------------------------------------------------------------------
129 # Magic exit status to denote no failure
135 - chown -R root /var/lib/postgresql/10/main
136 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/10/bin/initdb -D /shared/postgres_data || exit 0"
137 - chown -R postgres:postgres /shared/postgres_data
138 - chown -R postgres:postgres /var/run/postgresql
140 - exec: /root/upgrade_postgres
141 - exec: rm /root/upgrade_postgres
144 filename: "/etc/postgresql/10/main/postgresql.conf"
145 from: "data_directory = '/var/lib/postgresql/10/main'"
146 to: "data_directory = '/shared/postgres_data'"
148 # listen on all interfaces
150 filename: "/etc/postgresql/10/main/postgresql.conf"
151 from: /#?listen_addresses *=.*/
152 to: "listen_addresses = '*'"
154 # sync commit off is faster and less spiky, also marginally less safe
156 filename: "/etc/postgresql/10/main/postgresql.conf"
157 from: /#?synchronous_commit *=.*/
158 to: "synchronous_commit = $db_synchronous_commit"
160 # default is 128MB which is way too small
162 filename: "/etc/postgresql/10/main/postgresql.conf"
163 from: /#?shared_buffers *=.*/
164 to: "shared_buffers = $db_shared_buffers"
166 # default is 1MB which is too small
168 filename: "/etc/postgresql/10/main/postgresql.conf"
169 from: /#?work_mem *=.*/
170 to: "work_mem = $db_work_mem"
174 filename: "/etc/postgresql/10/main/postgresql.conf"
175 from: /#?default_text_search_config *=.*/
176 to: "default_text_search_config = '$db_default_text_search_config'"
178 # Necessary to enable backups
181 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
184 filename: "/etc/postgresql/10/main/postgresql.conf"
185 from: /#?max_wal_senders *=.*/
186 to: "max_wal_senders = $db_max_wal_senders"
189 filename: "/etc/postgresql/10/main/postgresql.conf"
190 from: /#?wal_level *=.*/
191 to: "wal_level = $db_wal_level"
194 filename: "/etc/postgresql/10/main/postgresql.conf"
195 from: /#?checkpoint_segments *=.*/
196 to: "checkpoint_segments = $db_checkpoint_segments"
199 filename: "/etc/postgresql/10/main/postgresql.conf"
200 from: /#?logging_collector *=.*/
201 to: "logging_collector = $db_logging_collector"
204 filename: "/etc/postgresql/10/main/postgresql.conf"
205 from: /#?log_min_duration_statement *=.*/
206 to: "log_min_duration_statement = $db_log_min_duration_statement"
209 filename: "/etc/postgresql/10/main/pg_hba.conf"
210 from: /^#local +replication +postgres +peer$/
211 to: "local replication postgres peer"
213 # allow all to connect in with md5 auth
215 filename: "/etc/postgresql/10/main/pg_hba.conf"
216 from: /^host.*all.*all.*127.*$/
217 to: "host all all 0.0.0.0/0 md5"
221 # use fast shutdown for pg
223 cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main
225 # give db a few secs to start up
228 - exec: su postgres -c 'createdb $db_name' || true
229 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
230 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
231 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
232 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
233 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
234 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
235 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
238 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
239 cmd: sudo -u postgres psql $db_name
243 path: /var/lib/postgresql/take-database-backup
244 chown: postgres:postgres
249 FILENAME=/shared/postgres_backup/$ID.tar.gz
250 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
254 path: /var/spool/cron/crontabs/postgres
256 # m h dom mon dow command
258 #0 */4 * * * /var/lib/postgresql/take-database-backup
262 cmd: "echo postgres installed!"