| 1 | hooks: |
| 2 | before_code: |
| 3 | - replace: |
| 4 | filename: /etc/service/unicorn/run |
| 5 | from: "# postgres" |
| 6 | to: sv start postgres || exit 1 |
| 7 | |
| 8 | run: |
| 9 | - exec: mkdir -p /shared/postgres_run |
| 10 | - exec: chown postgres:postgres /shared/postgres_run |
| 11 | - exec: chmod 775 /shared/postgres_run |
| 12 | - exec: rm -fr /var/run/postgresql |
| 13 | - exec: ln -s /shared/postgres_run /var/run/postgresql |
| 14 | - exec: socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1 |
| 15 | - file: |
| 16 | path: /etc/service/postgres/run |
| 17 | chmod: "+x" |
| 18 | contents: | |
| 19 | #!/bin/sh |
| 20 | exec 2>&1 |
| 21 | exec sudo -u postgres /usr/lib/postgresql/9.2/bin/postmaster -D /etc/postgresql/9.2/main |
| 22 | |
| 23 | - exec: |
| 24 | cmd: |
| 25 | - chown -R root /var/lib/postgresql/9.2/main |
| 26 | - "[ ! -e /shared/postgres_data ] && install -d -m 0755 -o postgres -g postgres /shared/postgres_data && sudo -u postgres /usr/lib/postgresql/9.2/bin/initdb -D /shared/postgres_data || exit 0" |
| 27 | - chown -R postgres:postgres /shared/postgres_data |
| 28 | |
| 29 | - replace: |
| 30 | filename: "/etc/postgresql/9.2/main/postgresql.conf" |
| 31 | from: "data_directory = '/var/lib/postgresql/9.2/main'" |
| 32 | to: "data_directory = '/shared/postgres_data'" |
| 33 | |
| 34 | # listen on all interfaces |
| 35 | - replace: |
| 36 | filename: "/etc/postgresql/9.2/main/postgresql.conf" |
| 37 | from: /#?listen_addresses *=.*/ |
| 38 | to: "listen_addresses = '*'" |
| 39 | |
| 40 | # Necessary to enable backups |
| 41 | - exec: |
| 42 | cmd: |
| 43 | - install -d -m 0755 -o postgres -g postgres /shared/postgres_backup |
| 44 | |
| 45 | - replace: |
| 46 | filename: "/etc/postgresql/9.2/main/postgresql.conf" |
| 47 | from: /#?max_wal_senders *=.*/ |
| 48 | to: "max_wal_senders = 4" |
| 49 | |
| 50 | - replace: |
| 51 | filename: "/etc/postgresql/9.2/main/postgresql.conf" |
| 52 | from: /#?wal_level *=.*/ |
| 53 | to: "wal_level = hot_standby" |
| 54 | |
| 55 | - replace: |
| 56 | filename: "/etc/postgresql/9.2/main/pg_hba.conf" |
| 57 | from: /^#local +replication +postgres +peer$/ |
| 58 | to: "local replication postgres peer" |
| 59 | |
| 60 | # allow all to connect in with md5 auth |
| 61 | - replace: |
| 62 | filename: "/etc/postgresql/9.2/main/pg_hba.conf" |
| 63 | from: /^host.*all.*all.*127.*$/ |
| 64 | to: "host all all 0.0.0.0/0 md5" |
| 65 | |
| 66 | - exec: |
| 67 | background: true |
| 68 | cmd: "sudo -u postgres /usr/lib/postgresql/9.2/bin/postmaster -D /etc/postgresql/9.2/main" |
| 69 | |
| 70 | # give db a few secs to start up |
| 71 | - exec: "sleep 5" |
| 72 | |
| 73 | - exec: sudo -u postgres createdb discourse || exit 0 |
| 74 | - exec: |
| 75 | stdin: | |
| 76 | create user discourse; |
| 77 | cmd: sudo -u postgres psql discourse |
| 78 | raise_on_fail: false |
| 79 | |
| 80 | - exec: |
| 81 | stdin: | |
| 82 | grant all privileges on database discourse to discourse; |
| 83 | cmd: sudo -u postgres psql discourse |
| 84 | raise_on_fail: false |
| 85 | |
| 86 | - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "alter schema public owner to discourse;"' |
| 87 | - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists hstore;"' |
| 88 | - exec: /bin/bash -c 'sudo -u postgres psql discourse <<< "create extension if not exists pg_trgm;"' |
| 89 | - exec: |
| 90 | stdin: | |
| 91 | update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'discourse' AND encoding = pg_encoding_to_char('SQL_ASCII'); |
| 92 | cmd: sudo -u postgres psql discourse |
| 93 | raise_on_fail: false |
| 94 | |
| 95 | - exec: |
| 96 | hook: postgres |
| 97 | cmd: "echo postgres installed!" |