gpt4 book ai didi

django - 如何在其Docker容器中执行psql交互?

转载 作者:行者123 更新时间:2023-12-02 18:45:48 26 4
gpt4 key购买 nike

我想在Postgres交互式 shell 程序中运行查询。我为此使用docker容器,如下所示:

这是docker-compose的相关部分:

  db_of_ivms:
image: postgres:10
restart: unless-stopped
ports:
- 5432:5432
container_name: db_of_ivms
environment:
POSTGRES_PASSWORD: xxx
POSTGRES_USER: ivms_usr
POSTGRES_DB: ivms_db

不过,我正在处理此错误:
docker exec -it -u 0 db_of_ivms bash
# psql

psql: FATAL: role "root" does not exist

最佳答案

您需要与postgres用户一起使用shell

docker exec -it -u postgres db_of_ivms bash

现在,对于您不指定 POSTGRES_USER: ivms_usr的容器:
postgres@d9b097b8db8c:/$ psql
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)


如果指定 POSTGRES_USER: ivms_usr和Database(请注意psql命令参数):
docker exec -it -u postgres db_of_ivms bash
postgres@2cec2435bb83:/$ psql -U ivms_usr -d ivms_db
psql (10.12 (Debian 10.12-1.pgdg90+1))
Type "help" for help.

ivms_db=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
ivms_db | ivms_usr | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | ivms_usr | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | ivms_usr | UTF8 | en_US.utf8 | en_US.utf8 | =c/ivms_usr +
| | | | | ivms_usr=CTc/ivms_usr
template1 | ivms_usr | UTF8 | en_US.utf8 | en_US.utf8 | =c/ivms_usr +
| | | | | ivms_usr=CTc/ivms_usr
(4 rows)

关于django - 如何在其Docker容器中执行psql交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260782/

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