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