airflow runs connections check against metadata db before init db - postgresql

I am running airflow locally based on dockerfile, .env, docker-compose.yaml and entrypoint.sh
as "docker-compose -f docker-compose.yaml up"
And just after "airflow init db" in entrypoint.sh I am getting the following error:
[after it all is cool, I can run airflow. But this drives me crazy. Can anyone help me to resolve it, please?]
what's strange is that service queries the tables even before the db has been initiated
airflow_webserver | initiating db
airflow_webserver | DB: postgresql://airflow:***#airflow_metadb:5432/airflow
airflow_webserver | [2022-02-22 13:52:26,318] {db.py:929} INFO - Dropping tables that exist
airflow_webserver | [2022-02-22 13:52:26,570] {migration.py:201} INFO - Context impl PostgresqlImpl.
airflow_webserver | [2022-02-22 13:52:26,570] {migration.py:204} INFO - Will assume transactional DDL.
airflow_metadb | 2022-02-22 13:52:26.712 UTC [71] ERROR: relation "connection" does not exist at character 55
airflow_metadb | 2022-02-22 13:52:26.712 UTC [71] STATEMENT: SELECT connection.conn_id AS connection_conn_id
airflow_metadb | FROM connection GROUP BY connection.conn_id
airflow_metadb | HAVING count(*) > 1
airflow_metadb | 2022-02-22 13:52:26.714 UTC [72] ERROR: relation "connection" does not exist at character 55
airflow_metadb | 2022-02-22 13:52:26.714 UTC [72] STATEMENT: SELECT connection.conn_id AS connection_conn_id
airflow_metadb | FROM connection
airflow_metadb | WHERE connection.conn_type IS NULL
airflow_webserver | [2022-02-22 13:52:26,733] {db.py:921} INFO - Creating tables
airflow 2.2.3
postgres 13
in dockerfile:
ENTRYPOINT ["/entrypoint.sh"]
in docker-compose.yaml:
webserver:
env_file: ./.env
image: airflow
container_name: airflow_webserver
restart: always
depends_on:
- postgres
environment:
<<: *env_common
AIRFLOW__CORE__LOAD_EXAMPLES: ${AIRFLOW__CORE__LOAD_EXAMPLES}
AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: ${AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION}
EXECUTOR: ${EXECUTOR}
_AIRFLOW_DB_UPGRADE: ${_AIRFLOW_DB_UPGRADE}
_AIRFLOW_WWW_USER_CREATE: ${_AIRFLOW_WWW_USER_CREATE}
_AIRFLOW_WWW_USER_USERNAME: ${_AIRFLOW_WWW_USER_USERNAME}
_AIRFLOW_WWW_USER_PASSWORD: ${_AIRFLOW_WWW_USER_PASSWORD}
_AIRFLOW_WWW_USER_ROLE: ${_AIRFLOW_WWW_USER_ROLE}
_AIRFLOW_WWW_USER_EMAIL: ${_AIRFLOW_WWW_USER_EMAIL}
logging:
options:
max-size: 10m
max-file: "3"
volumes:
- ./dags:bla-bla
- ./logs:bla-bla
ports:
- ${AIRFLOW_WEBSERVER_PORT}:${AIRFLOW_WEBSERVER_PORT}
command: webserver
healthcheck:
test: ["CMD-SHELL", "[ -f /usr/local/airflow/airflow-webserver.pid ]"]
interval: 30s
timeout: 30s
retries: 3

Related

Airflow in docker: Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries

I was following this example and I ran into the error after docker-compose up
airflow-airflow_scheduler-1 | Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries
airflow-airflow_scheduler-1 exited with code 1
airflow-airflow-1 | Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries
airflow-airflow_worker-1 | Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries
airflow-airflow-1 exited with code 1
airflow-airflow_worker-1 exited with code 1
My docker-compose.yml file looks like this:
# SPDX-License-Identifier: Apache-2.0
version: "3.7"
services:
airflow:
image: bitnami/airflow:1.10.13
ports:
- "8080:8080"
env_file:
- openlineage.env
environment:
- AIRFLOW_USERNAME=airflow
- AIRFLOW_PASSWORD=airflow
- AIRFLOW_EMAIL=airflow#example.com
- AIRFLOW_FERNET_KEY=Z2uDm0ZL60fXNkEXG8LW99Ki2zf8wkmIltaTz1iQPDU=
- AIRFLOW_DATABASE_HOST=postgres
- AIRFLOW_DATABASE_NAME=airflow
- AIRFLOW_DATABASE_USERNAME=airflow
- AIRFLOW_DATABASE_PASSWORD=airflow
- AIRFLOW_EXECUTOR=CeleryExecutor
- AIRFLOW_LOAD_EXAMPLES=no
- AIRFLOW_CONN_EXAMPLE_DB=postgres://example:example#postgres:5432/example
volumes:
- ./dags:/opt/bitnami/airflow/dags
- ${PWD}/whl:/whl
- type: bind
source: ${PWD}/requirements.txt
target: /bitnami/python/requirements.txt
airflow_scheduler:
image: bitnami/airflow-scheduler:1.10.13
env_file:
- openlineage.env
environment:
- AIRFLOW_FERNET_KEY=Z2uDm0ZL60fXNkEXG8LW99Ki2zf8wkmIltaTz1iQPDU=
- AIRFLOW_DATABASE_HOST=postgres
- AIRFLOW_DATABASE_NAME=airflow
- AIRFLOW_DATABASE_USERNAME=airflow
- AIRFLOW_DATABASE_PASSWORD=airflow
- AIRFLOW_EXECUTOR=CeleryExecutor
- AIRFLOW_LOAD_EXAMPLES=no
- AIRFLOW_CONN_EXAMPLE_DB=postgres://example:example#postgres:5432/example
volumes:
- ./dags:/opt/bitnami/airflow/dags
- ${PWD}/whl:/whl
- type: bind
source: ${PWD}/requirements.txt
target: /bitnami/python/requirements.txt
airflow_worker:
image: bitnami/airflow-worker:1.10.13
env_file:
- openlineage.env
environment:
- AIRFLOW_FERNET_KEY=Z2uDm0ZL60fXNkEXG8LW99Ki2zf8wkmIltaTz1iQPDU=
- AIRFLOW_DATABASE_HOST=postgres
- AIRFLOW_DATABASE_NAME=airflow
- AIRFLOW_DATABASE_USERNAME=airflow
- AIRFLOW_DATABASE_PASSWORD=airflow
- AIRFLOW_EXECUTOR=CeleryExecutor
- AIRFLOW_LOAD_EXAMPLES=no
- AIRFLOW_CONN_EXAMPLE_DB=postgres://example:example#postgres:5432/example
volumes:
- ./dags:/opt/bitnami/airflow/dags
- ${PWD}/whl:/whl
- type: bind
source: ${PWD}/requirements.txt
target: /bitnami/python/requirements.txt
marquez:
image: marquezproject/marquez:latest
ports:
- "5000:5000"
- "5001:5001"
volumes:
- ./docker/wait-for-it.sh:/usr/src/app/wait-for-it.sh
depends_on:
- postgres
entrypoint: ["./wait-for-it.sh", "postgres:5432", "--", "./entrypoint.sh"]
# Enables SQL statement logging (see: https://www.postgresql.org/docs/12/runtime-config-logging.html#GUC-LOG-STATEMENT)
# command: ["postgres", "-c", "log_statement=all"]
marquez_web:
image: marquezproject/marquez-web:latest
environment:
- MARQUEZ_HOST=marquez
- MARQUEZ_PORT=5000
ports:
- "3000:3000"
stdin_open: true
tty: true
depends_on:
- marquez
postgres:
image: bitnami/postgresql:12.1.0
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- AIRFLOW_USER=airflow
- AIRFLOW_PASSWORD=airflow
- AIRFLOW_DB=airflow
- MARQUEZ_USER=marquez
- MARQUEZ_PASSWORD=marquez
- MARQUEZ_DB=marquez
- EXAMPLE_USER=example
- EXAMPLE_PASSWORD=example
- EXAMPLE_DB=example
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- ./docker/init-db.sh:/docker-entrypoint-initdb.d/init-db.sh
redis:
image: bitnami/redis:6.0.6
environment:
- ALLOW_EMPTY_PASSWORD=yes
What confuses me is that postgres seems to be ready according to airflow-postgres
airflow-postgres-1 | postgresql 05:58:32.34 INFO ==> Starting PostgreSQL in background...
airflow-postgres-1 | postgresql 05:58:32.46 INFO ==> Changing password of postgres
airflow-postgres-1 | postgresql 05:58:32.48 INFO ==> Configuring replication parameters
airflow-postgres-1 | postgresql 05:58:32.51 INFO ==> Configuring fsync
airflow-postgres-1 | postgresql 05:58:32.51 INFO ==> Loading custom scripts...
airflow-postgres-1 | postgresql 05:58:32.52 INFO ==> Loading user's custom files from /docker-entrypoint-initdb.d ...
airflow-postgres-1 | postgresql 05:58:32.52 INFO ==> Starting PostgreSQL in background...
airflow-postgres-1 | postgresql 05:58:32.82 INFO ==> Enabling remote connections
airflow-postgres-1 | postgresql 05:58:32.83 INFO ==> Stopping PostgreSQL...
airflow-postgres-1 | postgresql 05:58:33.84 INFO ==> ** PostgreSQL setup finished! **
airflow-postgres-1 |
airflow-postgres-1 | postgresql 05:58:33.90 INFO ==> ** Starting PostgreSQL **
airflow-postgres-1 | 2022-06-08 05:58:33.924 GMT [1] LOG: starting PostgreSQL 12.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
airflow-postgres-1 | 2022-06-08 05:58:33.924 GMT [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
airflow-postgres-1 | 2022-06-08 05:58:33.924 GMT [1] LOG: listening on IPv6 address "::", port 5432
airflow-postgres-1 | 2022-06-08 05:58:33.931 GMT [1] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
airflow-postgres-1 | 2022-06-08 05:58:33.947 GMT [216] LOG: database system was shut down at 2022-06-08 05:58:32 GMT
airflow-postgres-1 | 2022-06-08 05:58:33.957 GMT [1] LOG: database system is ready to accept connections
But it seems like airflow-marquez simply can't connect to it:
airflow-marquez-1 | wait-for-it.sh: timeout occurred after waiting 15 seconds for postgres:5432
airflow-marquez-1 | WARNING 'MARQUEZ_CONFIG' not set, using development configuration.
I'm on Linux Manjaro.
I'm very new to docker and maybe I haven't included enough information for my problem, please point that out if it's true and I'll include more information.
Thanks in advance!

NestJS app build with docker can't access postgres database in the same docker network: ECONNREFUSED 127.0.0.1:5432

I want to run my app on my local machine within Docker. I don't want to optimize the size of my docker app or build it for production now.
Docker builds my backend-api app and postgres-db successfully. But I can only access the docker postgres database outside my docker e.g. with dbeaver installed on my computer. Also if I start my backend-api app WITHOUT Docker with "npm run start", my app can also access the database without any errors and can also write into the posgres db. Only when I build the backend-api with Docker and launch the app inside the Docker, I get this error. Since this only happens within Docker, I assume that something important is missing in my Dockerfile.
My docker-compose.yml:
version: '3'
services:
backend:
build: .
container_name: backend-api
command: npm run start
restart: unless-stopped
ports:
- 3000:3000
volumes:
- .:/usr/src/backend
networks:
- docker-network
depends_on:
- database
database:
image: postgres:latest
container_name: backend-db
ports:
- 5432:5432
volumes:
- postgresdb/:/var/lib/postgresql/data/
networks:
- docker-network
environment:
POSTGRES_USER: devuser
POSTGRES_PASSWORD: devpw
POSTGRES_DB: devdb
volumes:
postgresdb:
networks:
docker-network:
driver: bridge
My Dockerfile:
FROM node:16.15.0-alpine
WORKDIR /usr/src/backend
COPY . .
RUN npm install
sudo docker-compose up --build output:
Starting backend-db ... done
Recreating backend-api ... done
Attaching to backend-db, backend-api
backend-db |
backend-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
backend-db |
backend-db | 2022-05-03 20:35:46.065 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
backend-db | 2022-05-03 20:35:46.066 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
backend-db | 2022-05-03 20:35:46.066 UTC [1] LOG: listening on IPv6 address "::", port 5432
backend-db | 2022-05-03 20:35:46.067 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
backend-db | 2022-05-03 20:35:46.071 UTC [26] LOG: database system was shut down at 2022-05-03 20:35:02 UTC
backend-db | 2022-05-03 20:35:46.077 UTC [1] LOG: database system is ready to accept connections
backend-api |
backend-api | > backend#0.0.1 start
backend-api | > nodemon
backend-api |
backend-api | [nodemon] 2.0.15
backend-api | [nodemon] to restart at any time, enter `rs`
backend-api | [nodemon] watching path(s): src/**/*
backend-api | [nodemon] watching extensions: ts
backend-api | [nodemon] starting `IS_TS_NODE=true ts-node -r tsconfig-paths/register src/main.ts`
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [NestFactory] Starting Nest application...
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +73ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] AppModule dependencies initialized +0ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
backend-api | Error: connect ECONNREFUSED 127.0.0.1:5432
backend-api | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
backend-api | [Nest] 30 - 05/03/2022, 8:35:53 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
My ormconfig.ts:
import { ConnectionOptions } from 'typeorm';
const config: ConnectionOptions = {
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'devuser',
password: 'devpw',
database: 'devdb',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: false,
migrations: [__dirname + '/**/migrations/**/*{.ts,.js}'],
cli: {
migrationsDir: 'src/migrations',
},
};
export default config;
Please let me know what other information you would like me to provide. I'm new to the Docker world.
When you run a docker container localhost results in the container's localhost, not your machine's, so it's not the right host to use. As you're using docker-compose, a docker network is automatically created using the service's names as hosts. So instead of using localhost for you database host, you can use database as the host, and now the docker network will route the request properly to the database service as defined in your docker-compose.yml file
Ran into the same issue and this docker-compose.yml setup worked for me
version: "3.8"
services:
database:
container_name: db
image: postgres:14.2
ports:
- "5432:5432"
environment:
- POSTGRES_HOST_AUTH_METHOD
backend:
container_name: api
build:
dockerfile: Dockerfile
context: .
restart: on-failure
depends_on:
- database
ports:
- "3000:3000"
environment:
- DATABASE_HOST=database
- DATABASE_PORT=5432
- DATABASE_USER=postgres
- DATABASE_PASSWORD
- DATABASE_NAME
- DATABASE_SYNCHRONIZE
- NODE_ENV
Following were two main notable changes
changed the host to database so the docker network will route the
request properly to the database service as defined in your
docker-compose.yml file.
added a user postgres as default
PostgreSQL user

Pgadmin container kills postgres when using docker-compose

I run postgres using docker-compose -f postgres.yml up. When I try to run pgadmin4 using docker-compose -f pgadmin4.yml up, postgres container is automatically killed.
I am new to docker and doesn't understand why is this issue happening?
postgres.yml
version: "3.7"
services:
postgres:
container_name: "postgres"
image: "postgres"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: ****
volumes:
- maple:/var/lib/postgresql/data
volumes:
maple:
name: maple
external: true
pgadmin.yml
version: "3.7"
services:
postgres:
container_name: "pgadmin4"
image: "dpage/pgadmin4"
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: ******
PGADMIN_DEFAULT_PASSWORD: *****
volumes:
- pgadmin4-vol:/var/lib/pgadmin
volumes:
pgadmin4-vol:
name: pgadmin4-vol
external: true
$ docker-compose -f psql.yml up
Starting postgres ... done
Attaching to postgres
postgres |
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres |
postgres | 2020-11-27 10:18:05.635 UTC 1 LOG: starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres | 2020-11-27 10:18:05.636 UTC 1 LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2020-11-27 10:18:05.636 UTC 1 LOG: listening on IPv6 address "::", port 5432
postgres | 2020-11-27 10:18:05.639 UTC 1 LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2020-11-27 10:18:05.643 UTC [24] LOG: database system was shut down at 2020-11-27 10:17:59 UTC
postgres | 2020-11-27 10:18:05.647 UTC 1 LOG: database system is ready to accept connections
postgres | 2020-11-27 10:18:11.089 UTC 1 LOG: received fast shutdown request
postgres | 2020-11-27 10:18:11.092 UTC 1 LOG: aborting any active transactions
postgres | 2020-11-27 10:18:11.099 UTC 1 LOG: background worker "logical replication launcher" (PID 30) exited with exit code 1
postgres | 2020-11-27 10:18:11.108 UTC [25] LOG: shutting down
postgres | 2020-11-27 10:18:11.127 UTC 1 LOG: database system is shut down
postgres exited with code 0
$ docker-compose -f pgadmin.yml up
Recreating postgres ... done
Attaching to pgadmin4
pgadmin4 | sudo: setrlimit(RLIMIT_CORE): Operation not permitted
pgadmin4 | [2020-11-27 10:18:13 +0000] 1 [INFO] Starting gunicorn 19.9.0
pgadmin4 | [2020-11-27 10:18:13 +0000] 1 [INFO] Listening at: http://[::]:80 (1)
pgadmin4 | [2020-11-27 10:18:13 +0000] 1 [INFO] Using worker: threads
pgadmin4 | /usr/local/lib/python3.8/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
pgadmin4 | return io.open(fd, *args, **kwargs)
pgadmin4 | [2020-11-27 10:18:13 +0000] [87] [INFO] Booting worker with pid: 87
docker inpsect 69438bebc3f9 here
your services are both named postgres
so the pgadmin.yml recreates the service postgres, wich is your first database container.. as the logs are clearly showing
'Recreating postgres ... done'
take another name for the pgadmin service then postgres and it should work.
version: "3.7"
services:
pgadmin:
also docker-compose is for building stacks, if you would have done this in one compose file you would have seen the error from begin:
docker-compose.failing.yml:
version: "3.7"
services:
postgres:
container_name: "postgres"
image: "postgres"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: test
volumes:
- maple:/var/lib/postgresql/data
postgres:
container_name: "pgadmin4"
image: "dpage/pgadmin4"
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: test
PGADMIN_DEFAULT_PASSWORD: test
volumes:
- pgadmin4-vol:/var/lib/pgadmin
volumes:
maple:
name: maple
external: true
pgadmin4-vol:
name: pgadmin4-vol
external: true
docker-compose.working.yml:
version: "3.7"
services:
postgres:
container_name: "postgres"
image: "postgres"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: test
volumes:
- maple:/var/lib/postgresql/data
pgadmin:
container_name: "pgadmin4"
image: "dpage/pgadmin4"
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: test
PGADMIN_DEFAULT_PASSWORD: test
volumes:
- pgadmin4-vol:/var/lib/pgadmin
volumes:
maple:
name: maple
external: true
pgadmin4-vol:
name: pgadmin4-vol
external: true

Docker: no connection between postgres with node

I am trying to connect a postgres DB with Node api, with docker compose like this:
version: '3.8'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: biobox
volumes:
- ./db-data:/var/lib/postgresql/data
ports:
- 5432:5432
restart: always
api:
build: .
depends_on:
- db
ports:
- 3333:3333
command: yarn dev:server
env_file:
- .env
volumes:
- .:/usr/src/app
working_dir: /usr/src/app
env
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres
DATABASE=biobox
DATABASE_HOST=db
DATABASE_PORT=5432
dockerfile
FROM node:12.16.2-alpine
COPY package*.json ./
RUN yarn
COPY . .
COPY .env .
EXPOSE 3333
My api and db are online on container and the logs show no error.
because when i try make a query return this:
Using ts-node version 8.8.2, typescript version 3.8.3
api_1 | Create a new user User {
api_1 | completName: 'Name',
api_1 | email: 'Name#gmail.com',
api_1 | cpf: '23425',
api_1 | dateNasc: '10/10/2020',
api_1 | number: 'Namefd222',
api_1 | password: '1Name5234'
api_1 | }
db_1 | 2020-05-31 16:37:00.199 UTC [74] LOG: unexpected EOF on client connection with an open transaction
I don't understand why this happens. My environment variables are in accordance with the docker-compose create, in this case the database, USER, PASS.

How to connect to PostgreSQL using docker-compose?

Want to use docker-compose to run api application and postgresql database together.
docker-compose file:
version: '3'
volumes:
database_data:
driver: local
services:
db:
image: postgres:latest
volumes:
- database_data:/var/lib/postgresql/data
api:
build: ./api
expose:
- 8080
ports:
- 8080:8080
volumes:
- ./api:/usr/src/app/
links:
- db
environment:
- PGHOST=db
- PGDATABASE=postgres
- PGUSER=postgres
Api main.go file:
func main() {
db, err = gorm.Open("postgres", "host=db port=5432 user=postgres dbname=postgres")
// ...
}
When run the services, got message from log:
api_1 | [GIN] 2018/06/22 - 07:31:10 | 404 | 1.4404ms | 172.20.0.1 | GET /posts
api_1 |
api_1 | (sql: database is closed)
api_1 | [2018-06-22 07:31:10]
api_1 |
api_1 | (sql: database is closed)
api_1 | [2018-06-22 07:31:10]
api_1 | [GIN] 2018/06/22 - 07:32:14 | 403 | 15.6µs | 172.20.0.1 | GET /posts
db_1 | 2018-06-22 07:34:27.296 UTC [81] FATAL: role "root" does not exist
db_1 | 2018-06-22 07:34:36.897 UTC [90] FATAL: role "root" does not exist
Does this way not good? host=db in the connection string? Since db is the docker compose service name.
Add
It can work:
https://docs.docker.com/samples/library/postgres/#-or-via-psql
In the link you provided there are configuration settings that you did not added
restart: always
environment:
POSTGRES_PASSWORD: example
You should try this
version: '3'
services:
db:
image: postgres:latest
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: 'postgres'
volumes:
- database_data:/var/lib/postgresql/data
api:
build: ./api
expose:
- 8080
ports:
- 8080:8080
volumes:
- ./api:/usr/src/app/
links:
- db
environment:
- PGHOST: 'db'
- PGDATABASE: 'postgres'
- PGUSER: 'postgres'
- PGPASSWORD: 'postgres'
volumes:
database_data:
driver: local
and
db, err := gorm.Open("postgres", "host=db port=5432 user=postgres dbname=postgres password=postgres")