Example using Postgres

Example Yaml configuration for a project with a Postgres service dependency. Note that the postgres service will be accessible at database:5432.

pipeline:
  test:
    image: golang
    commands:
      - go get
      - go test

services:
  database:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_DB=test

The official Postgres image provides environment variables used at startup to create the default username, password, database and more. Please see the official image documentation for more details.

services:
  database:
    image: postgres
    environment:
+     - POSTGRES_USER=postgres
+     - POSTGRES_DB=test

If you are unable to connect to the Postgres container please make sure you are giving Postgres adequate time to initialize and begin accepting connections.

pipeline:
  test:
    image: golang
    commands:
+     - sleep 15
      - go get
      - go test

services:
  database:
    image: postgres

If you are still unable to connect to the Postgres container, please make sure you are using the service name as the hostname.

- psql -U root -d test -h 127.0.0.1:5432
+ psql -U root -d test -h tcp://database:5432

Working Example

This is a fully functioning example that demonstrates launching the Postgres service and then connecting with the service from the pipeline.

pipeline:
  ping:
    image: postgres
    commands:
      - sleep 10
      - |
        psql -U postgres -d test -h database \
          -c "SELECT * FROM pg_catalog.pg_tables;"

services:
  database:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_DB=test

Questions?

We are always happy to help with questions you might have. Search our documentation or check out answers to common questions. You can also post questions or comments to our community forum.