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
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 [ ! "10" = "$PG_MAJOR_OLD" ]; then
63 echo Upgrading PostgreSQL from version ${PG_MAJOR_OLD} to 10
64 free_disk=$(df /var | tail -n 1 | awk '{print $4}')
65 required=$(($(du -s /shared/postgres_data | awk '{print $1}') * 2))
67 if [ "$free_disk" -lt "$required" ]; then
68 echo "WARNING: Upgrading PostgresSQL would require an addtional $(du -sh /shared/postgres_data | awk '{print $1}') of disk space"
69 echo "Please free up some space, or expand your disk, before continuing."
73 rm -fr /shared/postgres_data_new
74 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
76 apt-get install -y postgresql-${PG_MAJOR_OLD}
77 chown -R postgres:postgres /var/lib/postgresql/10
78 /etc/init.d/postgresql stop
79 rm -fr /shared/postgres_data/postmaster.pid
81 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
82 echo >> /shared/postgres_data/postgresql.conf
83 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
85 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
87 if [[ "$SUCCESS" == 'false' ]]; then
88 echo -------------------------------------------------------------------------------------
89 echo UPGRADE OF POSTGRES FAILED
91 echo You are going to need to export your data and import into a clean instance:
94 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
95 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml"'
99 echo Run "./launcher rebuild app" again
101 echo When your instance is running:
102 echo Run "./launcher enter app"
103 echo Run apt-get remove postgresql-client-9.5 && apt-get instatll postgresql-client-10
104 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
107 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
108 echo Undo the base_image in your container config
111 echo Run: "./launcher stop app"
112 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
113 echo Run: "./launcher rebuild app"
115 echo Run: "./launcher enter app"
116 echo Run: "cd /shared/postgres_backup"
117 echo Run: "sv stop unicorn"
118 echo Run: "sudo -iu postgres dropdb discourse"
119 echo Run: "sudo -iu postgres createdb discourse"
120 echo Run: "sudo -iu postgres psql discourse < backup.db"
122 echo Run: "./launcher rebuild app"
126 mv /shared/postgres_data /shared/postgres_data_old
127 mv /shared/postgres_data_new /shared/postgres_data
129 echo -------------------------------------------------------------------------------------
130 echo UPGRADE OF POSTGRES COMPLETE
132 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
134 echo To complete the upgrade, rebuild again using:
136 echo ./launcher rebuild app
137 echo -------------------------------------------------------------------------------------
138 # Magic exit status to denote no failure
144 - chown -R root /var/lib/postgresql/10/main
145 - "[ ! -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"
146 - chown -R postgres:postgres /shared/postgres_data
147 - chown -R postgres:postgres /var/run/postgresql
149 - exec: /root/upgrade_postgres
150 - exec: rm /root/upgrade_postgres
153 filename: "/etc/postgresql/10/main/postgresql.conf"
154 from: "data_directory = '/var/lib/postgresql/10/main'"
155 to: "data_directory = '/shared/postgres_data'"
157 # listen on all interfaces
159 filename: "/etc/postgresql/10/main/postgresql.conf"
160 from: /#?listen_addresses *=.*/
161 to: "listen_addresses = '*'"
163 # sync commit off is faster and less spiky, also marginally less safe
165 filename: "/etc/postgresql/10/main/postgresql.conf"
166 from: /#?synchronous_commit *=.*/
167 to: "synchronous_commit = $db_synchronous_commit"
169 # default is 128MB which is way too small
171 filename: "/etc/postgresql/10/main/postgresql.conf"
172 from: /#?shared_buffers *=.*/
173 to: "shared_buffers = $db_shared_buffers"
175 # default is 1MB which is too small
177 filename: "/etc/postgresql/10/main/postgresql.conf"
178 from: /#?work_mem *=.*/
179 to: "work_mem = $db_work_mem"
183 filename: "/etc/postgresql/10/main/postgresql.conf"
184 from: /#?default_text_search_config *=.*/
185 to: "default_text_search_config = '$db_default_text_search_config'"
187 # Necessary to enable backups
190 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
193 filename: "/etc/postgresql/10/main/postgresql.conf"
194 from: /#?max_wal_senders *=.*/
195 to: "max_wal_senders = $db_max_wal_senders"
198 filename: "/etc/postgresql/10/main/postgresql.conf"
199 from: /#?wal_level *=.*/
200 to: "wal_level = $db_wal_level"
203 filename: "/etc/postgresql/10/main/postgresql.conf"
204 from: /#?checkpoint_segments *=.*/
205 to: "checkpoint_segments = $db_checkpoint_segments"
208 filename: "/etc/postgresql/10/main/postgresql.conf"
209 from: /#?logging_collector *=.*/
210 to: "logging_collector = $db_logging_collector"
213 filename: "/etc/postgresql/10/main/postgresql.conf"
214 from: /#?log_min_duration_statement *=.*/
215 to: "log_min_duration_statement = $db_log_min_duration_statement"
218 filename: "/etc/postgresql/10/main/pg_hba.conf"
219 from: /^#local +replication +postgres +peer$/
220 to: "local replication postgres peer"
222 # allow all to connect in with md5 auth
224 filename: "/etc/postgresql/10/main/pg_hba.conf"
225 from: /^host.*all.*all.*127.*$/
226 to: "host all all 0.0.0.0/0 md5"
230 # use fast shutdown for pg
232 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
234 # give db a few secs to start up
237 - exec: su postgres -c 'createdb $db_name' || true
238 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
239 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
240 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
241 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
242 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
243 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
244 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
247 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
248 cmd: sudo -u postgres psql $db_name
252 path: /var/lib/postgresql/take-database-backup
253 chown: postgres:postgres
258 FILENAME=/shared/postgres_backup/$ID.tar.gz
259 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
263 path: /var/spool/cron/crontabs/postgres
265 # m h dom mon dow command
267 #0 */4 * * * /var/lib/postgresql/take-database-backup
271 cmd: "echo postgres installed!"