diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..54684cc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Created by .ignore support plugin (hsz.mobi) + +.idea \ No newline at end of file diff --git a/README.md b/README.md index 9e49b985..46c2f542 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Don't forget the [Official documentation](https://ddev.readthedocs.io/en/stable/ ## Additional services added via docker-compose.\.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 diff --git a/docker-compose-services/postgres/README.md b/docker-compose-services/postgres/README.md new file mode 100644 index 00000000..02f0c28c --- /dev/null +++ b/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: + - :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. diff --git a/docker-compose-services/postgres/commands/postgres/pgsql_export b/docker-compose-services/postgres/commands/postgres/pgsql_export new file mode 100755 index 00000000..d40a667f --- /dev/null +++ b/docker-compose-services/postgres/commands/postgres/pgsql_export @@ -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" diff --git a/docker-compose-services/postgres/commands/postgres/pgsql_import b/docker-compose-services/postgres/commands/postgres/pgsql_import new file mode 100755 index 00000000..610fbc87 --- /dev/null +++ b/docker-compose-services/postgres/commands/postgres/pgsql_import @@ -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" diff --git a/docker-compose-services/postgres/docker-compose.postgres.yaml b/docker-compose-services/postgres/docker-compose.postgres.yaml new file mode 100644 index 00000000..4b38ca08 --- /dev/null +++ b/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