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/12-main.pg_stat_tmp
32 - exec: chown postgres:postgres /shared/postgres_run/12-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/12/bin/postmaster -D /etc/postgresql/12/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 [ ! "12" = "$PG_MAJOR_OLD" ]; then
64 echo Upgrading PostgreSQL from version ${PG_MAJOR_OLD} to 12
65 free_disk=$(df -P -B1 /shared | tail -n 1 | awk '{print $4}')
66 required=$(($(du -sb /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.10.template.yml" in containers/app.yml'
76 if [ -d /shared/postgres_data_old ]; then
77 mv /shared/postgres_data_old /shared/postgres_data_older
80 rm -fr /shared/postgres_data_new
81 install -d -m 0755 -o postgres -g postgres /shared/postgres_data_new && sudo -u postgres /usr/lib/postgresql/12/bin/initdb -D /shared/postgres_data_new || exit 0
83 apt-get install -y postgresql-${PG_MAJOR_OLD}
84 chown -R postgres:postgres /var/lib/postgresql/12
85 /etc/init.d/postgresql stop
86 rm -fr /shared/postgres_data/postmaster.pid
88 cp -pr /etc/postgresql/${PG_MAJOR_OLD}/main/* /shared/postgres_data
89 echo >> /shared/postgres_data/postgresql.conf
90 echo "data_directory = '/shared/postgres_data'" >> /shared/postgres_data/postgresql.conf
92 sudo -u postgres /usr/lib/postgresql/12/bin/pg_upgrade -d /shared/postgres_data -D /shared/postgres_data_new -b /usr/lib/postgresql/${PG_MAJOR_OLD}/bin -B /usr/lib/postgresql/12/bin || SUCCESS=false
94 if [[ "$SUCCESS" == 'false' ]]; then
95 echo -------------------------------------------------------------------------------------
96 echo UPGRADE OF POSTGRES FAILED
98 echo Please visit https://meta.discourse.org/t/postgresql-12-update/151236 for support
100 echo You can run "./launcher start app" to restart your app in the meanwhile
105 mv /shared/postgres_data /shared/postgres_data_old
106 mv /shared/postgres_data_new /shared/postgres_data
108 echo -------------------------------------------------------------------------------------
109 echo UPGRADE OF POSTGRES COMPLETE
111 echo Old ${PG_MAJOR_OLD} database is stored at /shared/postgres_data_old
113 echo To complete the upgrade, rebuild again using:
115 echo ./launcher rebuild app
116 echo -------------------------------------------------------------------------------------
117 # Magic exit status to denote no failure
123 - chown -R root /var/lib/postgresql/12/main
124 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/12/bin/initdb -D /shared/postgres_data || exit 0"
125 - chown -R postgres:postgres /shared/postgres_data
126 - chown -R postgres:postgres /var/run/postgresql
128 - exec: /root/upgrade_postgres
129 - exec: rm /root/upgrade_postgres
132 filename: "/etc/postgresql/12/main/postgresql.conf"
133 from: "data_directory = '/var/lib/postgresql/12/main'"
134 to: "data_directory = '/shared/postgres_data'"
136 # listen on all interfaces
138 filename: "/etc/postgresql/12/main/postgresql.conf"
139 from: /#?listen_addresses *=.*/
140 to: "listen_addresses = '*'"
142 # sync commit off is faster and less spiky, also marginally less safe
144 filename: "/etc/postgresql/12/main/postgresql.conf"
145 from: /#?synchronous_commit *=.*/
146 to: "synchronous_commit = $db_synchronous_commit"
148 # default is 128MB which is way too small
150 filename: "/etc/postgresql/12/main/postgresql.conf"
151 from: /#?shared_buffers *=.*/
152 to: "shared_buffers = $db_shared_buffers"
154 # default is 1MB which is too small
156 filename: "/etc/postgresql/12/main/postgresql.conf"
157 from: /#?work_mem *=.*/
158 to: "work_mem = $db_work_mem"
162 filename: "/etc/postgresql/12/main/postgresql.conf"
163 from: /#?default_text_search_config *=.*/
164 to: "default_text_search_config = '$db_default_text_search_config'"
166 # Necessary to enable backups
169 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
172 filename: "/etc/postgresql/12/main/postgresql.conf"
173 from: /#?max_wal_senders *=.*/
174 to: "max_wal_senders = $db_max_wal_senders"
177 filename: "/etc/postgresql/12/main/postgresql.conf"
178 from: /#?wal_level *=.*/
179 to: "wal_level = $db_wal_level"
182 filename: "/etc/postgresql/12/main/postgresql.conf"
183 from: /#?checkpoint_segments *=.*/
184 to: "checkpoint_segments = $db_checkpoint_segments"
187 filename: "/etc/postgresql/12/main/postgresql.conf"
188 from: /#?logging_collector *=.*/
189 to: "logging_collector = $db_logging_collector"
192 filename: "/etc/postgresql/12/main/postgresql.conf"
193 from: /#?log_min_duration_statement *=.*/
194 to: "log_min_duration_statement = $db_log_min_duration_statement"
197 filename: "/etc/postgresql/12/main/pg_hba.conf"
198 from: /^#local +replication +postgres +peer$/
199 to: "local replication postgres peer"
201 # allow all to connect in with md5 auth
203 filename: "/etc/postgresql/12/main/pg_hba.conf"
204 from: /^host.*all.*all.*127.*$/
205 to: "host all all 0.0.0.0/0 md5"
209 # use fast shutdown for pg
211 cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/12/bin/postmaster -D /etc/postgresql/12/main
213 # give db a few secs to start up
216 - exec: su postgres -c 'createdb $db_name' || true
217 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
218 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
219 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
220 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
221 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
222 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
223 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
226 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
227 cmd: sudo -u postgres psql $db_name
231 path: /var/lib/postgresql/take-database-backup
232 chown: postgres:postgres
237 FILENAME=/shared/postgres_backup/$ID.tar.gz
238 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
242 path: /var/spool/cron/crontabs/postgres
244 # m h dom mon dow command
246 #0 */4 * * * /var/lib/postgresql/take-database-backup
250 cmd: "echo postgres installed!"