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