9

I'm running Postgres DB with pg-admin and GO on the docker-compose.

Problem: I can connect from pg-admin to Postgres. But cannot establish a connection from Go.

I tried different combinations of authentication string but it does not work. String format same as here https://github.com/karlkeefer/pngr - but different container name - database

(ERROR) Connection URl:

backend_1         | 2021/08/08 14:24:40 DB connection: database://main:fugZwypczB94m0LP7CcH@postgres:5432/temp_db?sslmode=disable
backend_1         | 2021/08/08 14:24:40 Unalble to open DB connection: dial tcp 127.0.0.1:5432: connect: connection refused

(URI generation same as here https://github.com/karlkeefer/pngr)

Docker:

version: '3.8'
services:
  backend:
    restart: always
    build: 
      context: backend
      target: dev
    volumes:
      - ./backend:/root
    ports:
      - "5000:5000"
    env_file: .env
    depends_on: 
      - database
  database:
    build: database
    restart: always
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      PGDATA: /var/lib/postgresql/data
    volumes:
      - ./database/data:/var/lib/postgresql/data
      - ./logs/databse:/var/log/postgresql
      - ./database/migrations:/docker-entrypoint-initdb.d/migrations
    ports:
      - "5432:5432"
  database-admin:
    image: dpage/pgadmin4:5.5
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PG_ADMIN_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PG_ADMIN_PASSWORD}
      PGADMIN_LISTEN_PORT: 80
    ports:
      - "8080:80"
    volumes:
      - ./database/admin:/var/lib/pgadmin
    links:
      - "database:pgsql-server"
    depends_on: 
      - database
volumes:
  database:
  database-admin:

Environment:

POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=temp_db
POSTGRES_USER=main
POSTGRES_PASSWORD=fugZwypczB94m0LP7CcH
[email protected]
PG_ADMIN_PASSWORD=ayzi2ta8f1TnX3vKQSN1
PG_ADMIN_PORT=80

GO Code:

db, err = sqlx.Open("postgres", str)

str

func buildConnectionString() string {
    user := os.Getenv("POSTGRES_USER")
    pass := os.Getenv("POSTGRES_PASSWORD")
    if user == "" || pass == "" {
        log.Fatalln("You must include POSTGRES_USER and POSTGRES_PASSWORD environment variables")
    }
    host := os.Getenv("POSTGRES_HOST")
    port := os.Getenv("POSTGRES_PORT")
    dbname := os.Getenv("POSTGRES_DB")
    if host == "" || port == "" || dbname == "" {
        log.Fatalln("You must include POSTGRES_HOST, POSTGRES_PORT, and POSTGRES_DB environment variables")
    }

    str := fmt.Sprintf("database://%s:%s@%s:%s/%s?sslmode=disable", user, pass, host, port, dbname)

    log.Println("DB connection: " + str)

    return str
}

Thanks in advance!

0

1 Answer 1

11

You reference the database hostname as postgres (POSTGRES_HOST=postgres) which is fine, but the container/service name is database.

Either change the name in your compose.yaml from database to postgres or add an explicit hostname field:

database:
  build: database
  restart: always
  hostname: postgres   # <- add this

You may also want to add a dedicated network for multiple container services to talk to one another (or prevent others from). To do this, add this to each service your want to use a specific network e.g.

database:
  # ...

  networks:
    - mynet

backend:
  # ...

  networks:
    - mynet

and define the network at the end of your compose.yaml

networks:
  mynet:
    name: my-shared-db-network
2
  • Hi @colm.anseo, Thanks for hint!
    – ilbets
    Commented Aug 8, 2021 at 15:53
  • Just tried, also update URI to start with postgres and it worked! Thanks! Just changing host-name in env. file to database did not work, I assume it is by design? (really confused)
    – ilbets
    Commented Aug 8, 2021 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.