gpt4 book ai didi

postgresql - 连接到在Docker中运行的postgres时出错:pq:用户“postgres”的密码身份验证失败

转载 作者:行者123 更新时间:2023-12-01 22:41:28 25 4
gpt4 key购买 nike

尝试打开数据库,但说用户“postgres”的密码身份验证失败
我找不到这个问题的根本原因。第一次,我正在使用Docker。求助

func openDB() (*sqlx.DB, error) {
q := url.Values{}
q.Set("sslmode", "disable")
q.Set("timezone", "utc")

u := url.URL{
Scheme: "postgres",
User: url.UserPassword("postgres", "postgres"),
Host: "localhost",
Path: "postgres",
RawQuery: q.Encode(),
}
fmt.Println(u.String())

// fmt.Println(u.String()) is
// postgre://postgres:postgres@localhost/postgres?sslmode=disable&timezone=utc
return sqlx.Open("postgres", u.String())
}
docker-compose.yaml 看起来像这样。
version: '3'
networks:
shared-network:
driver: bridge
services:
db:
container_name: sales_db
networks:
- shared-network
image: postgres:11.1-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432

最佳答案

代码没有问题。我更改了端口,现在一切正常。

func openDB() (*sqlx.DB, error) {
q := url.Values{}
q.Set("sslmode", "disable")
q.Set("timezone", "utc")

u := url.URL{
Scheme: "postgres",
User: url.UserPassword("postgres", "postgres"),
Host: "localhost:5433", // change here
Path: "postgres",
RawQuery: q.Encode(),
}
fmt.Println(u.String())

// fmt.Println(u.String()) is
// postgre://postgres:postgres@localhost/postgres?sslmode=disable&timezone=utc
return sqlx.Open("postgres", u.String())
}
version: '3'
networks:
shared-network:
driver: bridge
services:
db:
container_name: sales_db
networks:
- shared-network
image: postgres:11.1-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5433:5432 //change here

关于postgresql - 连接到在Docker中运行的postgres时出错:pq:用户“postgres”的密码身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62536977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com