Skip to content

Commit

Permalink
Postgres README improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Irving Popovetsky <[email protected]>
  • Loading branch information
Irving Popovetsky committed Nov 20, 2017
1 parent faaded0 commit a806e42
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ hab start core/postgresql
```
or
```
docker run starkandwayne/postgresql
docker run chefdemo/postgresql
```
if you want to bring up the pre-exported docker image.

Expand All @@ -22,7 +22,7 @@ This plan supports running clustered PostgreSQL by utilizing Habitat's native le

You can run an example cluster via docker-compose after exporting a docker container from this plan:
```
$ hab pkg export docker core/postgresql
$ hab pkg export docker $(ls -1t results/*.hart | head -1)
```

The docker post-process should create a docker image named `core/postgresql` and it should be available on your local machine
Expand All @@ -33,22 +33,35 @@ version: '3'
services:
pg1:
image: core/postgresql
command: "start core/postgresql --group cluster --topology leader"
image: chefdemo/postgresql
command: --group cluster
--topology leader
volumes:
- pg1-data:/hab/svc/postgresql/data
pg2:
image: core/postgresql
command: "start core/postgresql --group cluster --topology leader --peer pg1"
image: chefdemo/postgresql
command: --group cluster
--topology leader
--peer pg1
volumes:
- pg2-data:/hab/svc/postgresql/data
pg3:
image: core/postgresql
command: "start core/postgresql --group cluster --topology leader --peer pg1"
image: chefdemo/postgresql
command: --group cluster
--topology leader
--peer pg1
volumes:
- pg3-data:/hab/svc/postgresql/data
volumes:
pg1-data:
pg2-data:
pg3-data:
EOF
docker-compose up
```

We intend to support a prod ready clustering solution similar to [patroni](https://github.com/zalando/patroni) and are working on getting there quickly.


## Binding

Consuming services can bind to PostgreSQL via:
Expand All @@ -59,13 +72,18 @@ hab start <origin>/<app> --bind database:postgresql.default --peer <pg-host>

Superuser access is exposed to consumers when binding and we advise that required databases, schemas and roles be created and migrations get run in the init hook of the consuming service. The created roles (with restricted access) should then be exposed to the application code that will get run.

To support binding to either standalone or custered PostgreSQL services we suggest using the `.first` field of the binding in the handlebars interpolation:
To support binding to either standalone or custered PostgreSQL services we suggest using the `eachAlive` and `@first` helpers of the binding in the handlebars interpolation:
```
{{#with bind.database.first as |pg| }}
PGPASSWORD={{pg.cfg.superuser_password}}
PGUSER={{pg.cfg.superuser_name}}
PGHOST={{pg.sys.ip}}
{{/with}}
{{~ #if bind.database}}
{{~ #eachAlive bind.database.members as |member|}}
{{~ #if @first}}
PGHOST="{{member.sys.ip}}"
PGPORT="{{member.cfg.port}}"
PGUSER="{{member.cfg.superuser_name}}"
PGPASSWORD="{{member.cfg.superuser_password}}"
{{~ /if}}
{{~ /eachAlive}}
{{~ /if}}
```

`.first` will always point to the leader when present.
In clustered situations, there is no reliable way to determine the leader via template helpers. However, in census data the members are ordered chronologically by join time.

0 comments on commit a806e42

Please sign in to comment.