无法连接到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:
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.
我试图运行docker compose来设置nginx,golang服务器和postgresql。 问题是我无法连接到数据库: p>
最初,我 试图通过以下指令连接到postgres: p>
db,err = gorm.Open(“ postgres”,“ host = db port = 5432 user = sigbrian password = example sslmode = disable“)
code> pre>
使用此docker-compose文件: p>
版本:'3.2'
\ n#卷:
#数据库数据:
#驱动程序:本地
服务:
数据库:
图像:postgres
重新启动:始终
环境:
-POSTGRES_DB:sigdb
-POSTGRES_USER:sigbrian
-POSTGRES_PASSWORD:example
端口:
-'5432:5432'
#卷:
#-database_data:/ var / lib / postgresql / data
golang-app:
构建:
上下文 :。
dockerfile:Dockerfile
#命令:[“ ./wait-for-it.sh”,“ db:5432”]
#端口:
#-'80:8888'
expo se:
-'8080'
depends_on:
-“ db”
链接:
-“ db”
nginx代理和网络服务器:
图片:nginx:1.15.5-alpine \ n个端口:
-'80:80'
卷:
-./nginx.conf:/etc/nginx/nginx.conf
-./static:/var/www/sig/static
取决于 :
-“ golang-app”
链接:
-“ golang-app”
code> pre>
但是当我评论postgres用户,数据库和密码环境时 文件中的变量,并尝试以这种方式建立连接: p>
db,err = gorm.Open(“ postgres”,“ host = db port = 5432 user = postgres sslmode = disable“)
code> pre>
仍然抛出相同的错误。 感谢您的帮助。 p>
div>
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