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

标签 django postgresql docker docker-compose psql

我想在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/

相关文章:

django - 在django模板中显示查询集对象

基于 Django 类的仅发布 View

sql - 寻找只购买没有其他人购买的商品的客户

sql - 一组日期的标准差

sql - postgresql 轮除法

python - 是否可以在生成 500 时向 django 发送给管理员的电子邮件添加 header ?

python - 使用 Django 将 Assets 通过管道传输到 CDN 的最佳方式是什么?

docker - 安装 scikit-image 时没有名为 'numpy' 的模块

angular - Docker和Angular CLI应用

azure - 通过 SSH 连接到 Docker 容器的 Azure 应用服务