将golang容器连接到postgres时出现此错误。
pq:无法检测默认用户名。请明确提供一个
我尝试了很多用户名和密码组合,但是没有运气。我应该使用哪种用户名组合?
docker-compose代码为:
version: '3.6'
services:
postgres:
image: postgres:11.1-alpine
ports:
- '5432:5432'
#network_mode: bridge
container_name: postgres
environment:
POSTGRES_USER: 'user'
POSTGRES_PASSWORD: 'password'
POSTGRESS_DB: 'db_amex01'
volumes:
- ./init:/docker-entrypoint-initdb.d/
todo:
build: ./go_amex/
depends_on:
- "postgres"
restart: always
# ports:
# - "8000:8080"
./ go_amex / main.go
db, err := gorm.Open("postgres", "host='postgres'&user=user@localhost&port=5432&dbname='todo'&password='password'&sslmode=disable")
if err != nil {
fmt.Println("============ exiting ==========")
fmt.Println(err)
panic("failed to connect database cc")
}
defer db.Close()
答案 0 :(得分:0)
尝试一下:
version: '3'
services:
postgres:
image: kartoza/postgis:9.6-2.4
ports:
- "5432:5432"
environment:
POSTGRES_USER: developer
POSTGRES_DBNAME: somedb
POSTGRES_PASS: somepass
volumes:
- $HOME/data/postgres:/var/lib/postgresql/data
在Yaml中配置数据库
sqlBind := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
"developer",
"somepass",
"somedb",
)