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 4 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 @@ -15,6 +15,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)
* [PostreSQL](docker-compose-services/postgres/README.md)

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

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

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

Choose a reason for hiding this comment

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

(Why?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In most of our project, we need some kind of spatial search (locations around you, locations in polygon, distance between locations, ... ) and it's really easy and fast with PostGIS. Until activated, there's zero performance penalty.

Copy link
Member

Choose a reason for hiding this comment

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

I was saying to say "why" in the README or docker-compose file. So people can understand why you made the choice (and maybe understand whether they should also be using this, or maybe the library version)


### 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 `command/postgres` to your project
dacostafilipe marked this conversation as resolved.
Show resolved Hide resolved
3. *(optional)* Update your config.yaml file to support auto-import/auto-export (see bellow)
dacostafilipe marked this conversation as resolved.
Show resolved Hide resolved

### Connection

Connect to `postgres` with
dacostafilipe marked this conversation as resolved.
Show resolved Hide resolved

```
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 `import-db/postgresql.db.sql`
- `ddev pgsql_import` : Use `pgsql` to import `import-db/postgresql.db.sql` into `db`

To import/export the `db` table automatically add the following hooks to your `config.yaml`:
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't a docker volume suit most people better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't remember why we did it that way, will look into this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, seems like it was just an example of calculated laziness. We needed the import/export commands and they "just worked" for us when restarting projects.


```
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`;
```
@@ -0,0 +1,3 @@
#!/bin/bash

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,3 @@
#!/bin/bash

su postgres -c "psql -U db db < /mnt/ddev_config/import-db/postgresql.db.sql"
20 changes: 20 additions & 0 deletions docker-compose-services/postgres/docker-compose.postgres.yaml
@@ -0,0 +1,20 @@
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
com.ddev.app-url: $DDEV_URL
dacostafilipe marked this conversation as resolved.
Show resolved Hide resolved
web:
links:
- postgres:postgres
dacostafilipe marked this conversation as resolved.
Show resolved Hide resolved