我正在尝试设置postgres容器,并希望使用以下方法设置postgres登录:
POSTGRES_USER: docker
POSTGRES_PASSWORD: docker因此,我创建了如下所示的dockerCompose.yml
web:
build: .
ports:
- "62576:62576"
links:
- redis
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: docker
POSTGRES_USER: docker
redis:
image: redis我还尝试了环境变量的其他语法,将db部分声明为:
db:
image: postgres
environment:
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker但是,这两个选项似乎都不起作用,因为无论出于什么原因,每当我试图使用各种连接字符串连接到postgres数据库时:
postgres://postgres:postgres@db:5432/users
postgres://postgres:docker@db:5432/users
postgres://docker:docker@db:5432/users它们都给了我失败的机会,而不是抱怨没有用户数据库。
发布于 2017-02-04 17:58:40
我在这方面挣扎了一段时间,没有找到被接受的答案,我终于通过移除容器来让它开始工作:
docker-compose rm postgres然后是卷:
docker volume rm myapp_postgres然后,当我做一个新的docker-compose up时,我看到CREATE ROLE飞来飞去,我想这就是最初的up错过了什么。
本文从这里、Git对邮政码头的官方形象等方面阐述了其原因。
发布于 2020-01-07 15:26:55
如果您正在使用Docker。
尝试检查您的本地DB是否处于活动状态,因为它主要是与Docker冲突的,如果是的话,您可以禁用它或更改端口号或卸载它,以避免冲突。
发布于 2015-04-12 22:13:16
您得到的身份验证错误将有很大帮助!
我用你的论点激发了postgres的形象:
docker run --name db -d -e POSTGRES_PASSWORD=docker -e POSTGRES_USER=docker postgres然后我执行了:
docker exec -it db psql -U docker user
psql: FATAL: database "user" does not exist我收到您期望的错误消息,因为我具有信任身份验证:
docker exec -it db cat /var/lib/postgresql/data/pg_hba.conf | grep -v '^#'
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 md5为了模拟web容器,我将运行postgres容器的另一个实例并链接db容器,然后连接回db容器:
core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker user
Password for user docker:
psql: FATAL: password authentication failed for user "docker"如果输入错误的密码,将得到一个身份验证错误。但是,如果我输入正确的密码:
core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker user
Password for user docker:
psql: FATAL: database "user" does not exist一切似乎都在正常工作。我将其全部放入yaml文件中,并以这种方式对其进行了测试:
web:
image: postgres
command: sleep 999
ports:
- "62576:62576"
links:
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: docker
POSTGRES_USER: docker然后用“码头工”启动它:
core@ku1 /tmp/i $ docker-compose -f dc.yaml up
Creating i_db_1...
Creating i_web_1...
Attaching to i_db_1, i_web_1
db_1 | ok
db_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
db_1 | initializing pg_authid ... ok
db_1 | initializing dependencies ... ok
db_1 | creating system views ... ok
db_1 | loading system objects' descriptions ... ok
db_1 | creating collations ... ok
db_1 | creating conversions ... ok
db_1 | creating dictionaries ... ok
db_1 | setting privileges on built-in objects ... ok
db_1 | creating information schema ... ok
db_1 | loading PL/pgSQL server-side language ... ok
db_1 | vacuuming database template1 ... ok
db_1 | copying template1 to template0 ... ok
db_1 | copying template1 to postgres ... ok
db_1 | syncing data to disk ... ok
db_1 |
db_1 | WARNING: enabling "trust" authentication for local connections
db_1 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --auth-local and --auth-host, the next time you run initdb.
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | postgres -D /var/lib/postgresql/data
db_1 | or
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 |
db_1 | PostgreSQL stand-alone backend 9.4.1
db_1 | backend> statement: CREATE DATABASE "docker" ;
db_1 |
db_1 | backend>
db_1 |
db_1 | PostgreSQL stand-alone backend 9.4.1
db_1 | backend> statement: CREATE USER "docker" WITH SUPERUSER PASSWORD 'docker' ;
db_1 |
db_1 | backend>
db_1 | LOG: database system was shut down at 2015-04-12 22:01:12 UTC
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
^Z
[1]+ Stopped docker-compose -f dc.yaml up
core@ku1 /tmp/i $ bg您可以看到创建了用户和密码。我执行以下任务:
core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db user
Password for user docker:
psql: FATAL: password authentication failed for user "docker"
core@ku1 /tmp/i $
db_1 | FATAL: password authentication failed for user "docker"
db_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"
core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db user
Password for user docker:
psql: FATAL: database "user" does not exist
db_1 | FATAL: database "user" does not exist因此,我唯一能想到的是,您正在尝试从您的主机连接到数据库,而不是web容器?还是您的web容器没有使用“db”作为要连接的主机?您对web容器的定义不包含我可以看到的任何错误。
https://stackoverflow.com/questions/29580798
复制相似问题