Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add PostgreSQL configuration example (#7)
  • Loading branch information
dacostafilipe authored and rfay committed Aug 23, 2019
1 parent 6cd72be commit aeacfb3
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
# Created by .ignore support plugin (hsz.mobi)

.idea
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -16,6 +16,7 @@ Don't forget the [Official documentation](https://ddev.readthedocs.io/en/stable/
## Additional services added via docker-compose.\<service\>.yaml
* [MongoDB](docker-compose-services/mongodb/README.md)
* [Blackfire.io](docker-compose-services/blackfire/README.md)
* [PostreSQL](docker-compose-services/postgres/README.md)

## .ddev/web-build/Dockerfile examples to customize web container

Expand Down
62 changes: 62 additions & 0 deletions docker-compose-services/postgres/README.md
@@ -0,0 +1,62 @@
## PostgreSQL

Using PostgreSQL container with [PostGIS](https://postgis.net/) support provided by [mdillon/postgis](https://hub.docker.com/r/mdillon/postgis).

### Installation

1. Copy `docker-compose.postgres.yaml` to your project
2. Copy the full `commands/postgres` directory to your project's `.ddev/commands` directory. For example `cp -r commands/postgres /.ddev/commands`
3. *(optional)* Update your config.yaml file to support auto-import/auto-export (see below)

### Connection

Connect to `postgres` host/db server from within the web container with:

```
Host: postgres
User: db
Password: db
Database: db
```

For external access, use the port used in your `docker-compose.postgres.yaml` and `127.0.0.1` as host.

When using multiple project with PostgreSQL support, remember to update your `docker-compose.postgres.yaml` to use different ports:

```
ports:
- <EXTERNAL_PORT>:5432
```

### Import / Export

Two new `ddev` commands are provided:

- `ddev pgsql_export` : Use `pg_dump` to export `db` to `.ddev/import-db/postgresql.db.sql`
- `ddev pgsql_import` : Use `pgsql` to import `.ddev/import-db/postgresql.db.sql` into `db` - Note that this must be executed with an empty database.

Example `config.yaml` hooks configuration to automatically import/export the `db` table:

```
hooks:
pre-stop:
- exec-host: ddev pgsql_export
post-start:
- exec-host: ddev pgsql_import
```

### PostGIS

The `postgres` image support `postgis`, but you will need to create the extension before using it:

```
CREATE EXTENSION IF NOT EXISTS `postgis`;
```

### TODO

Future enhancements (PR's welcome here) include:

* Non-volatile postgres database (store it on a docker volume like ddev's normal mariadb container does)
* Provide interactive custom commands to interact with the `postgres` utility in the container interactively.
* Consider changing suggested import/export hooks into "exec" hooks with "service: postgres" instead of running `ddev pg*` on the host.
@@ -0,0 +1,7 @@
#!/bin/bash

## Description: Dump the postgresql database to .ddev/import-db/postgresql.db.sql
## Usage: pgsql_export
## Example: ddev pgsql_export

mkdir -p /mnt/ddev_config/import-db && su postgres -c "pg_dump db --username=db --host=localhost --port=5432 --column-inserts > /mnt/ddev_config/import-db/postgresql.db.sql"
@@ -0,0 +1,8 @@
#!/bin/bash

## Description: Load to an empty postgresql database from .ddev/import-db/postgresql.db.sql
## Usage: pgsql_import
## Example: ddev pgsql_import

# Import via user postgres to avoid credentials prompt
su postgres -c "psql -U db db < /mnt/ddev_config/import-db/postgresql.db.sql"
19 changes: 19 additions & 0 deletions docker-compose-services/postgres/docker-compose.postgres.yaml
@@ -0,0 +1,19 @@
version: '3.6'
services:
postgres:
container_name: ddev-${DDEV_SITENAME}-postgres
image: mdillon/postgis:11
ports:
- 32784:5432
environment:
- POSTGRES_PASSWORD=db
- POSTGRES_USER=db
- POSTGRES_DB=db
volumes:
- ".:/mnt/ddev_config"
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.approot: $DDEV_APPROOT
web:
links:
- postgres:postgres

0 comments on commit aeacfb3

Please sign in to comment.