Search code examples
postgresqldockergodocker-compose

Unable to connect to Postgres running in docker


For a golang back-end, I am using Postgres as database everything works fine locally. But I can't seem to run the docker-compose file.

version: '3.8'

services:
  app:
    build: .
    ports:
      # - "8080:8080"
      - "${PORT}:${PORT}"
    depends_on:
      - db
    environment:
      - DB_CONN=postgres://postgres:password@db:5432/tutor-connect
      # - PORT=8080
      - PORT=${PORT}

    volumes:
      - ./cmd:/app/cmd
      - ./config:/app/config
      - ./internal:/app/internal
      - ./go.mod:/app/go.mod
      - ./go.sum:/app/go.sum
      - ./README.md:/app/README.md
    env_file:
      - .env

  db:
    image: postgres:16.2
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: tutor-connect
    ports:
      - "5433:5432"
    volumes:
      # - /docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Error found is:

tutor-backend-app-1  | DB STRING :  postgres://postgres:password@db:5432/tutor-connect
tutor-backend-app-1  | db string is : postgres://postgres:password@db:5432/tutor-connect
tutor-backend-app-1  | 
tutor-backend-app-1  | 2024/06/11 06:39:25 /app/internal/adapters/db/db.go:15
tutor-backend-app-1  | [error] failed to initialize database, got error failed to connect to `host=db user=postgres database=tutor-connect`: hostname resolving error (lookup db on 127.0.0.11:53: server misbehaving)
tutor-backend-app-1  | Error connecting to DB failed to connect to `host=db user=postgres database=tutor-connect`: hostname resolving error (lookup db on 127.0.0.11:53: server misbehaving)
tutor-backend-app-1  | 2024/06/11 06:39:25 Can't connect to database
tutor-backend-db-1 exited with code 1
tutor-backend-app-1 exited with code 1

Here is how I connect to the database:

err := godotenv.Load(".env")
dbString := os.Getenv("DB_CONN")
var dsn = flag.String("dsn", dbString, "Connection string to database")

I changed the .env value from which the application gets the connection string to the postgres db URL running in docker but it does not work.


Solution

  • The issue you meet is that the volume postgres_data already contain a database initialize with a postgres13 database.

    To fix the issue :

    or you rename the volume

      db:
        image: postgres:16.2
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: tutor-connect
        ports:
          - "5433:5432"
        volumes:
          - postgres_data2:/var/lib/postgresql/data
    
    volumes:
      postgres_data2:
    

    or you delete the volume

    docker volume rm postgres_data
    

    The idea of these two first solutions is to make reinitialize the database with postgres 16 engine

    If you want to reuse the initialized database in the postgres_data volume you can also downgrade the database engine to the needed version

    image: postgres:13