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 $(numfmt --to=si $(($required - $free_disk))) of disk space"
69 echo "Please free up some space, or expand your disk, before continuing."
71 echo 'To avoid upgrading change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml" in containers/app.yml'
75 rm -fr /shared/postgres_data_new
76 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
78 apt-get install -y postgresql-${PG_MAJOR_OLD}
79 chown -R postgres:postgres /var/lib/postgresql/10
80 /etc/init.d/postgresql stop
81 rm -fr /shared/postgres_data/postmaster.pid
83 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
84 echo >> /shared/postgres_data/postgresql.conf
85 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
87 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
89 if [[ "$SUCCESS" == 'false' ]]; then
90 echo -------------------------------------------------------------------------------------
91 echo UPGRADE OF POSTGRES FAILED
93 echo You are going to need to export your data and import into a clean instance:
96 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
97 echo 'In containers/app.yml: Change "templates/postgres.template.yml" TO "templates/postgres.9.5.template.yml"'
101 echo Run "./launcher rebuild app" again
103 echo When your instance is running:
104 echo Run "./launcher enter app"
105 echo Run apt-get remove postgresql-client-9.5 && apt-get instatll postgresql-client-10
106 echo Run "cd /shared/postgres_backup && sudo -u postgres pg_dump discourse > backup.db"
109 if [ "$PG_MAJOR_OLD" = "9.5" ]; then
110 echo Undo the base_image in your container config
113 echo Run: "./launcher stop app"
114 echo Run: "sudo mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old"
115 echo Run: "./launcher rebuild app"
117 echo Run: "./launcher enter app"
118 echo Run: "cd /shared/postgres_backup"
119 echo Run: "sv stop unicorn"
120 echo Run: "sudo -iu postgres dropdb discourse"
121 echo Run: "sudo -iu postgres createdb discourse"
122 echo Run: "sudo -iu postgres psql discourse < backup.db"
124 echo Run: "./launcher rebuild app"
128 mv /shared/postgres_data /shared/postgres_data_old
129 mv /shared/postgres_data_new /shared/postgres_data
131 echo -------------------------------------------------------------------------------------
132 echo UPGRADE OF POSTGRES COMPLETE
134 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
136 echo To complete the upgrade, rebuild again using:
138 echo ./launcher rebuild app
139 echo -------------------------------------------------------------------------------------
140 # Magic exit status to denote no failure
146 - chown -R root /var/lib/postgresql/10/main
147 - "[ ! -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"
148 - chown -R postgres:postgres /shared/postgres_data
149 - chown -R postgres:postgres /var/run/postgresql
151 - exec: /root/upgrade_postgres
152 - exec: rm /root/upgrade_postgres
155 filename: "/etc/postgresql/10/main/postgresql.conf"
156 from: "data_directory = '/var/lib/postgresql/10/main'"
157 to: "data_directory = '/shared/postgres_data'"
159 # listen on all interfaces
161 filename: "/etc/postgresql/10/main/postgresql.conf"
162 from: /#?listen_addresses *=.*/
163 to: "listen_addresses = '*'"
165 # sync commit off is faster and less spiky, also marginally less safe
167 filename: "/etc/postgresql/10/main/postgresql.conf"
168 from: /#?synchronous_commit *=.*/
169 to: "synchronous_commit = $db_synchronous_commit"
171 # default is 128MB which is way too small
173 filename: "/etc/postgresql/10/main/postgresql.conf"
174 from: /#?shared_buffers *=.*/
175 to: "shared_buffers = $db_shared_buffers"
177 # default is 1MB which is too small
179 filename: "/etc/postgresql/10/main/postgresql.conf"
180 from: /#?work_mem *=.*/
181 to: "work_mem = $db_work_mem"
185 filename: "/etc/postgresql/10/main/postgresql.conf"
186 from: /#?default_text_search_config *=.*/
187 to: "default_text_search_config = '$db_default_text_search_config'"
189 # Necessary to enable backups
192 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
195 filename: "/etc/postgresql/10/main/postgresql.conf"
196 from: /#?max_wal_senders *=.*/
197 to: "max_wal_senders = $db_max_wal_senders"
200 filename: "/etc/postgresql/10/main/postgresql.conf"
201 from: /#?wal_level *=.*/
202 to: "wal_level = $db_wal_level"
205 filename: "/etc/postgresql/10/main/postgresql.conf"
206 from: /#?checkpoint_segments *=.*/
207 to: "checkpoint_segments = $db_checkpoint_segments"
210 filename: "/etc/postgresql/10/main/postgresql.conf"
211 from: /#?logging_collector *=.*/
212 to: "logging_collector = $db_logging_collector"
215 filename: "/etc/postgresql/10/main/postgresql.conf"
216 from: /#?log_min_duration_statement *=.*/
217 to: "log_min_duration_statement = $db_log_min_duration_statement"
220 filename: "/etc/postgresql/10/main/pg_hba.conf"
221 from: /^#local +replication +postgres +peer$/
222 to: "local replication postgres peer"
224 # allow all to connect in with md5 auth
226 filename: "/etc/postgresql/10/main/pg_hba.conf"
227 from: /^host.*all.*all.*127.*$/
228 to: "host all all 0.0.0.0/0 md5"
232 # use fast shutdown for pg
234 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
236 # give db a few secs to start up
239 - exec: su postgres -c 'createdb $db_name' || true
240 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
241 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
242 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
243 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
244 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
245 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
246 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
249 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
250 cmd: sudo -u postgres psql $db_name
254 path: /var/lib/postgresql/take-database-backup
255 chown: postgres:postgres
260 FILENAME=/shared/postgres_backup/$ID.tar.gz
261 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
265 path: /var/spool/cron/crontabs/postgres
267 # m h dom mon dow command
269 #0 */4 * * * /var/lib/postgresql/take-database-backup
273 cmd: "echo postgres installed!"