Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add PostgreSQL configuration example (#7)
- Loading branch information
1 parent
6cd72be
commit aeacfb3
Showing
6 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
7 changes: 7 additions & 0 deletions
7
docker-compose-services/postgres/commands/postgres/pgsql_export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
8 changes: 8 additions & 0 deletions
8
docker-compose-services/postgres/commands/postgres/pgsql_import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
19
docker-compose-services/postgres/docker-compose.postgres.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |