Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PostgreSQL configuration example #7

Merged
merged 8 commits into from Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note: People will often want to use the 'php' project type, so that ddev doesn't naively try to mess with their database configuration (AdditionalConfiguration.php or settings.ddev.php, etc)

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a docker volume for the Postgres data so it's not volatile?

labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.approot: $DDEV_APPROOT
web:
links:
- postgres:postgres