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