简体   繁体   中英

Cant connect to postgres (password authentication failed for user - docker)

I was trying to run docker compose to set up nginx, golang server and postgresql. The problem is that I can't connect to the database:

在此处输入图片说明

Initially, I was trying to connect to postgres with this instruction:

db, err = gorm.Open("postgres", "host=db port=5432 user=sigbrian password=example sslmode=disable")

With this docker-compose file:

version: '3.2'

# volumes:
  # database_data:
    # driver: local

services:
  db:
    image: postgres
    restart: always
    environment:
      - POSTGRES_DB:sigdb
      - POSTGRES_USER:sigbrian
      - POSTGRES_PASSWORD:example
    ports:
      - '5432:5432'
    # volumes:
      # - database_data:/var/lib/postgresql/data
  golang-app:
    build:
      context: .
      dockerfile: Dockerfile
    # command: ["./wait-for-it.sh", "db:5432"]
    # ports:
    #   - '80:8888'
    expose:
      - '8080'
    depends_on:
      - "db"
    links:
      - "db"
  nginx-proxy-and-webserver:
    image: nginx:1.15.5-alpine
    ports:
      - '80:80'
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./static:/var/www/sig/static
    depends_on:
      - "golang-app"
    links:
      - "golang-app"

But when I comment the postgres user, database and password environment variables from the file, and try to make the connection this way:

db, err = gorm.Open("postgres", "host=db port=5432 user=postgres sslmode=disable")

Still throws the same error. I appreciate any help.

I'm using docker compose with postgres and golang like you, here is the docker-compose.yml file i use

version: '2'
services:
  redis:
    image: redis:latest
    command: --appendonly yes

  postgres:
    image: postgres:latest
    environment:
      - POSTGRES_USER=amine
      - POSTGRES_PASSWORD=amine
      - POSTGRES_DB=0000

  app:
    image: ahilaly/amine.in:v0.1.3
    command: -wait
    ports:
      - 7000:7000

    depends_on:
      - postgres
      - redis

    links:
      - postgres:postgres
      - redis:redis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM