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: locale-gen $LANG && update-locale
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/10-main.pg_stat_tmp
32 - exec: chown postgres:postgres /shared/postgres_run/10-main.pg_stat_tmp
34 path: /etc/service/postgres/run
39 HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/10/bin/postmaster -D /etc/postgresql/10/main
42 path: /etc/service/postgres/log/run
46 mkdir -p /var/log/postgres
47 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 from version ${PG_MAJOR_OLD} to 10
65 free_disk=$(df /shared | tail -n 1 | awk '{print $4}')
66 required=$(($(du -s /shared/postgres_data | awk '{print $1}') * 2))
68 if [ "$free_disk" -lt "$required" ]; then
69 echo "WARNING: Upgrading PostgresSQL would require an addtional $(numfmt --to=si $(($required - $free_disk))) of disk space"
70 echo "Please free up some space, or expand your disk, before continuing."
72 echo 'To avoid upgrading change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml" in containers/app.yml'
76 rm -fr /shared/postgres_data_new
77 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
79 apt-get install -y postgresql-${PG_MAJOR_OLD}
80 chown -R postgres:postgres /var/lib/postgresql/10
81 /etc/init.d/postgresql stop
82 rm -fr /shared/postgres_data/postmaster.pid
84 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
85 echo >> /shared/postgres_data/postgresql.conf
86 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
88 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
90 if [[ "$SUCCESS" == 'false' ]]; then
91 echo -------------------------------------------------------------------------------------
92 echo UPGRADE OF POSTGRES FAILED
94 echo You are going to need to export your data and import into a clean instance:
97 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
98 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml"'
102 echo Run "./launcher rebuild app" again
104 echo When your instance is running:
105 echo Run "./launcher enter app"
106 echo Run apt-get remove postgresql-client-9.5 && apt-get instatll postgresql-client-10
107 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
110 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
111 echo Undo the base_image in your container config
114 echo Run: "./launcher stop app"
115 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
116 echo Run: "./launcher rebuild app"
118 echo Run: "./launcher enter app"
119 echo Run: "cd /shared/postgres_backup"
120 echo Run: "sv stop unicorn"
121 echo Run: "sudo -iu postgres dropdb discourse"
122 echo Run: "sudo -iu postgres createdb discourse"
123 echo Run: "sudo -iu postgres psql discourse < backup.db"
125 echo Run: "./launcher rebuild app"
129 mv /shared/postgres_data /shared/postgres_data_old
130 mv /shared/postgres_data_new /shared/postgres_data
132 echo -------------------------------------------------------------------------------------
133 echo UPGRADE OF POSTGRES COMPLETE
135 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
137 echo To complete the upgrade, rebuild again using:
139 echo ./launcher rebuild app
140 echo -------------------------------------------------------------------------------------
141 # Magic exit status to denote no failure
147 - chown -R root /var/lib/postgresql/10/main
148 - "[ ! -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"
149 - chown -R postgres:postgres /shared/postgres_data
150 - chown -R postgres:postgres /var/run/postgresql
152 - exec: /root/upgrade_postgres
153 - exec: rm /root/upgrade_postgres
156 filename: "/etc/postgresql/10/main/postgresql.conf"
157 from: "data_directory = '/var/lib/postgresql/10/main'"
158 to: "data_directory = '/shared/postgres_data'"
160 # listen on all interfaces
162 filename: "/etc/postgresql/10/main/postgresql.conf"
163 from: /#?listen_addresses *=.*/
164 to: "listen_addresses = '*'"
166 # sync commit off is faster and less spiky, also marginally less safe
168 filename: "/etc/postgresql/10/main/postgresql.conf"
169 from: /#?synchronous_commit *=.*/
170 to: "synchronous_commit = $db_synchronous_commit"
172 # default is 128MB which is way too small
174 filename: "/etc/postgresql/10/main/postgresql.conf"
175 from: /#?shared_buffers *=.*/
176 to: "shared_buffers = $db_shared_buffers"
178 # default is 1MB which is too small
180 filename: "/etc/postgresql/10/main/postgresql.conf"
181 from: /#?work_mem *=.*/
182 to: "work_mem = $db_work_mem"
186 filename: "/etc/postgresql/10/main/postgresql.conf"
187 from: /#?default_text_search_config *=.*/
188 to: "default_text_search_config = '$db_default_text_search_config'"
190 # Necessary to enable backups
193 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
196 filename: "/etc/postgresql/10/main/postgresql.conf"
197 from: /#?max_wal_senders *=.*/
198 to: "max_wal_senders = $db_max_wal_senders"
201 filename: "/etc/postgresql/10/main/postgresql.conf"
202 from: /#?wal_level *=.*/
203 to: "wal_level = $db_wal_level"
206 filename: "/etc/postgresql/10/main/postgresql.conf"
207 from: /#?checkpoint_segments *=.*/
208 to: "checkpoint_segments = $db_checkpoint_segments"
211 filename: "/etc/postgresql/10/main/postgresql.conf"
212 from: /#?logging_collector *=.*/
213 to: "logging_collector = $db_logging_collector"
216 filename: "/etc/postgresql/10/main/postgresql.conf"
217 from: /#?log_min_duration_statement *=.*/
218 to: "log_min_duration_statement = $db_log_min_duration_statement"
221 filename: "/etc/postgresql/10/main/pg_hba.conf"
222 from: /^#local +replication +postgres +peer$/
223 to: "local replication postgres peer"
225 # allow all to connect in with md5 auth
227 filename: "/etc/postgresql/10/main/pg_hba.conf"
228 from: /^host.*all.*all.*127.*$/
229 to: "host all all 0.0.0.0/0 md5"
233 # use fast shutdown for pg
235 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
237 # give db a few secs to start up
240 - exec: su postgres -c 'createdb $db_name' || true
241 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
242 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
243 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
244 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
245 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
246 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
247 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
250 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
251 cmd: sudo -u postgres psql $db_name
255 path: /var/lib/postgresql/take-database-backup
256 chown: postgres:postgres
261 FILENAME=/shared/postgres_backup/$ID.tar.gz
262 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
266 path: /var/spool/cron/crontabs/postgres
268 # m h dom mon dow command
270 #0 */4 * * * /var/lib/postgresql/take-database-backup
274 cmd: "echo postgres installed!"