Bump base image (#538)
[discourse_docker.git] / templates / postgres.9.5.template.yml
1 params:
2 db_synchronous_commit: "off"
3 db_shared_buffers: "256MB"
4 db_work_mem: "10MB"
5 db_default_text_search_config: "pg_catalog.english"
6 db_name: discourse
7 db_user: discourse
8 db_wal_level: minimal
9 db_max_wal_senders: 0
10 db_checkpoint_segments: 6
11 db_logging_collector: off
12 db_log_min_duration_statement: 100
13
14 hooks:
15 before_code:
16 - replace:
17 filename: /etc/service/unicorn/run
18 from: "# postgres"
19 to: sv start postgres || exit 1
20
21 run:
22 - exec: apt-get remove -y postgresql-10 postgresql-client-10 postgresql-contrib-10
23 - exec: apt-get update && apt-get install -y postgresql-9.5 postgresql-client-9.5 postgresql-contrib-9.5
24 - exec: mkdir -p /shared/postgres_run
25 - exec: chown postgres:postgres /shared/postgres_run
26 - exec: chmod 775 /shared/postgres_run
27 - exec: rm -fr /var/run/postgresql
28 - exec: ln -s /shared/postgres_run /var/run/postgresql
29 - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
30 - exec: rm -fr /shared/postgres_run/.s*
31 - exec: rm -fr /shared/postgres_run/*.pid
32 - exec: mkdir -p /shared/postgres_run/9.5-main.pg_stat_tmp
33 - exec: chown postgres:postgres /shared/postgres_run/9.5-main.pg_stat_tmp
34 - file:
35 path: /etc/service/postgres/run
36 chmod: "+x"
37 contents: |
38 #!/bin/sh
39 exec 2>&1
40 echo -1000 >/proc/self/oom_score_adj
41 HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/9.5/bin/postmaster -D /etc/postgresql/9.5/main
42
43 - file:
44 path: /etc/runit/3.d/99-postgres
45 chmod: "+x"
46 contents: |
47 #!/bin/bash
48 sv stop postgres
49
50 - exec:
51 cmd:
52 - chown -R root /var/lib/postgresql/9.5/main
53 - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -E -u postgres /usr/lib/postgresql/9.5/bin/initdb -D /shared/postgres_data || exit 0"
54 - chown -R postgres:postgres /shared/postgres_data
55 - chown -R postgres:postgres /var/run/postgresql
56
57 - replace:
58 filename: "/etc/postgresql/9.5/main/postgresql.conf"
59 from: "data_directory = '/var/lib/postgresql/9.5/main'"
60 to: "data_directory = '/shared/postgres_data'"
61
62 # listen on all interfaces
63 - replace:
64 filename: "/etc/postgresql/9.5/main/postgresql.conf"
65 from: /#?listen_addresses *=.*/
66 to: "listen_addresses = '*'"
67
68 # sync commit off is faster and less spiky, also marginally less safe
69 - replace:
70 filename: "/etc/postgresql/9.5/main/postgresql.conf"
71 from: /#?synchronous_commit *=.*/
72 to: "synchronous_commit = $db_synchronous_commit"
73
74 # default is 128MB which is way too small
75 - replace:
76 filename: "/etc/postgresql/9.5/main/postgresql.conf"
77 from: /#?shared_buffers *=.*/
78 to: "shared_buffers = $db_shared_buffers"
79
80 # default is 1MB which is too small
81 - replace:
82 filename: "/etc/postgresql/9.5/main/postgresql.conf"
83 from: /#?work_mem *=.*/
84 to: "work_mem = $db_work_mem"
85
86 # allow for other
87 - replace:
88 filename: "/etc/postgresql/9.5/main/postgresql.conf"
89 from: /#?default_text_search_config *=.*/
90 to: "default_text_search_config = '$db_default_text_search_config'"
91
92 # Necessary to enable backups
93 - exec:
94 cmd:
95 - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup
96
97 - replace:
98 filename: "/etc/postgresql/9.5/main/postgresql.conf"
99 from: /#?max_wal_senders *=.*/
100 to: "max_wal_senders = $db_max_wal_senders"
101
102 - replace:
103 filename: "/etc/postgresql/9.5/main/postgresql.conf"
104 from: /#?wal_level *=.*/
105 to: "wal_level = $db_wal_level"
106
107 - replace:
108 filename: "/etc/postgresql/9.5/main/postgresql.conf"
109 from: /#?checkpoint_segments *=.*/
110 to: "checkpoint_segments = $db_checkpoint_segments"
111
112 - replace:
113 filename: "/etc/postgresql/9.5/main/postgresql.conf"
114 from: /#?logging_collector *=.*/
115 to: "logging_collector = $db_logging_collector"
116
117 - replace:
118 filename: "/etc/postgresql/9.5/main/postgresql.conf"
119 from: /#?log_min_duration_statement *=.*/
120 to: "log_min_duration_statement = $db_log_min_duration_statement"
121
122 - replace:
123 filename: "/etc/postgresql/9.5/main/pg_hba.conf"
124 from: /^#local +replication +postgres +peer$/
125 to: "local replication postgres peer"
126
127 # allow all to connect in with md5 auth
128 - replace:
129 filename: "/etc/postgresql/9.5/main/pg_hba.conf"
130 from: /^host.*all.*all.*127.*$/
131 to: "host all all 0.0.0.0/0 md5"
132
133 - exec:
134 background: true
135 # use fast shutdown for pg
136 stop_signal: INT
137 cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/9.5/bin/postmaster -D /etc/postgresql/9.5/main
138
139 # give db a few secs to start up
140 - exec: "sleep 5"
141
142 - exec: su postgres -c 'createdb $db_name' || true
143 - exec: su postgres -c 'psql $db_name -c "create user $db_user;"' || true
144 - exec: su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
145 - exec: su postgres -c 'psql $db_name -c "alter schema public owner to $db_user;"'
146 - exec: su postgres -c 'psql template1 -c "create extension if not exists hstore;"'
147 - exec: su postgres -c 'psql template1 -c "create extension if not exists pg_trgm;"'
148 - exec: su postgres -c 'psql $db_name -c "create extension if not exists hstore;"'
149 - exec: su postgres -c 'psql $db_name -c "create extension if not exists pg_trgm;"'
150 - exec:
151 stdin: |
152 update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name' AND encoding = pg_char_to_encoding('SQL_ASCII');
153 cmd: sudo -u postgres psql $db_name
154 raise_on_fail: false
155
156 - file:
157 path: /var/lib/postgresql/take-database-backup
158 chown: postgres:postgres
159 chmod: "+x"
160 contents: |
161 #!/bin/bash
162 ID=db-$(date +%F_%T)
163 FILENAME=/shared/postgres_backup/$ID.tar.gz
164 pg_basebackup --format=tar --pgdata=- --xlog --gzip --label=$ID > $FILENAME
165 echo $FILENAME
166
167 - file:
168 path: /var/spool/cron/crontabs/postgres
169 contents: |
170 # m h dom mon dow command
171 #MAILTO=?
172 #0 */4 * * * /var/lib/postgresql/take-database-backup
173
174 - exec:
175 hook: postgres
176 cmd: "echo postgres installed!"