英文:
P1000: Authentication failed against database server at `localhost`, the provided database credentials for `postgres` are not valid
问题
尝试连接到运行在Docker中的PostgreSQL(使用pgAdmin4或尝试使用Prisma进行数据库迁移),但结果出现以下错误消息:
错误:P1000:在`localhost`上的数据库服务器上的身份验证失败,为`postgres`提供的数据库凭据无效。
请确保为`localhost`上的数据库服务器提供有效的数据库凭据。
以下是来自Docker容器pg_container
的日志消息:
2023年7月10日23:28:55 2023年7月11日03:28:55.532 UTC [84]致命错误:用户“postgres”的密码验证失败
2023年7月10日23:28:55 2023年7月11日03:28:55.532 UTC [84]详细信息:角色“postgres”不存在。
2023年7月10日23:28:55 连接匹配了pg_hba.conf第100行:“host all all all scram-sha-256”
我的docker-compose.yml
:
services:
dev-db:
container_name: pg_container
image: postgres:latest
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=1234
- POSTGRES_DB=dev
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
运行docker ps
:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d31b7483c8ad postgres:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5432->5432/tcp pg_container
fa587cd32a04 dpage/pgadmin4 "/entrypoint.sh" 2 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:5050->80/tcp pgadmin4_container
我尝试过的:
- 尝试查看是否有后台运行的本地PostgreSQL实例,但没有找到任何内容。
英文:
Attempting to connect (with pgAdmin4 or attempting a DB migrate with Prisma) to my PostgreSQL running in docker, however, results in the error message
Error: P1000: Authentication failed against database server at `localhost`, the provided database credentials for `postgres` are not valid.
Please make sure to provide valid database credentials for the database server at `localhost`.
Here are the log message from the docker container pg_container
:
2023-07-10 23:28:55 2023-07-11 03:28:55.532 UTC [84] FATAL: password authentication failed for user "postgres"
2023-07-10 23:28:55 2023-07-11 03:28:55.532 UTC [84] DETAIL: Role "postgres" does not exist.
2023-07-10 23:28:55 Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
My docker-compose.yml
:
services:
dev-db:
container_name: pg_container
image: postgres:latest
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD= 1234
- POSTGRES_DB= dev
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
Running docker ps
:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d31b7483c8ad postgres:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5432->5432/tcp pg_container
fa587cd32a04 dpage/pgadmin4 "/entrypoint.sh" 2 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:5050->80/tcp pgadmin4_container
What I've tried:
- Try to see if I had a local instance of a postgres running in background, there's nothing
答案1
得分: 1
如果您使用environment:
的字符串列表形式,则每个项目都应该是KEY=value
,没有空白字符。如果有空白字符,则它将包含在键和/或值中。在您的示例中,用户名是带有前导空格的postgres
,数据库名称也类似地带有空格的dev
。
您可以尝试将带有空格的凭据输入到PGAdmin或Prisma中。
在Compose文件中,您要么需要删除这些空格
environment:
- POSTGRES_USER=postgres
# ^^ 这里没有空格
要么更改为YAML映射语法,这将更一致地使用YAML引用和转义规则
environment:
POSTGRES_USER: postgres
# ^ 无前导连字符,键和值之间用冒号
英文:
If you're using the list-of-strings form of environment:
then each item is exactly KEY=value
, with no whitespace. If there is whitespace then it is included in the key and/or value. In your example, the username is postgres
with a leading space, and the database name is similarly dev
with a space.
You can try entering these credentials with the spaces into PGAdmin or Prisma.
In the Compose file, you either need to remove the spaces
environment:
- POSTGRES_USER=postgres
# ^^ no space here
or change to the YAML map syntax, which will more consistently use YAML quoting and escaping rules
environment:
POSTGRES_USER: postgres
# ^ no leading hyphen, colon between key and value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论