dousao6260 2018-10-24 06:00 采纳率: 0%
浏览 823

无法连接到postgres(用户密码验证失败-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:

enter image description here

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.

  • 写回答

2条回答 默认 最新

  • doukun5339 2018-10-24 15:41
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘