【问题标题】:testing.postgresql command not found: initdb inside docker未找到 testing.postgresql 命令:docker 内的 initdb
【发布时间】:2020-01-03 08:13:12
【问题描述】:

您好,我正在尝试使用使用 sqlalchemy 和 alembic 的 postgresql 数据库进行单元测试

我也在 docker postgresql 上运行它

我正在按照 testing.postgresql(docs) 的文档设置一个临时的 postgresql 实例处理数据库测试,并编写了以下测试:

def test_crawl_bds_obj(self):
    with testing.postgresql.Postgresql() as postgresql:
        engine.create_engine(postgresql.url())
        result = crawl_bds_obj(1 ,'/ban-can-ho-chung-cu-duong-mai-chi-tho-phuong-an-phu-prj-the-sun-avenue/nh-chu-gap-mat-tien-t-q2-thuoc-du-tphcm-pr22171626')
        self.assertEqual(result, 'sell')

crawl_bds_obj() 基本上保存来自 URL 的信息并使用 session.commit() 将其保存到数据库并返回类型数据

当我尝试运行测试时,它返回以下错误:

ERROR: test_crawl_bds_obj (tests.test_utils.TestUtils)
raise RuntimeError("command not found: %s" % name)
RuntimeError: command not found: initdb

在文档中它说“testing.postgresql.Postgresql 在实例化时执行 initdb 和 postgres。在删除 Postgresql 对象时,它会终止 PostgreSQL 实例并删除临时目录。”

那么,当我已经安装了 testing.postgresql 并在我的 docker 上运行了 postgresql 时,为什么会出现 initdb 错误?

编辑:

我也设置了数据路径,但它仍然返回相同的错误

码头文件:

FROM python:slim-jessie
ADD requirements.txt /app/requirements.txt
ADD . /app/
WORKDIR /app/
RUN pip install -r requirements.txt

码头工人撰写:

  postgres:
    image: postgres
    restart: always
    environment:
      - POSTGRES_USER=${POSTGRES_DEFAULT_USER}
      - POSTGRES_PASSWORD=${POSTGRES_DEFAULT_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DEFAULT_DB}
      - POSTGRES_PORT=${POSTGRES_DEFAULT_PORT}
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
  pgadmin:
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
    volumes:
       - ./data/pgadmin:/root/.pgadmin
    ports:
      - "${PGADMIN_PORT}:80"
    logging:
      driver: none
    restart: unless-stopped

  worker:
    build:
      context: .
      dockerfile: Dockerfile
    command: "watchmedo auto-restart --recursive -p '*.py'"
    environment:
      - C_FORCE_ROOT=1
    volumes:
      - .:/app
    links:
      - rabbit
    depends_on:
      - rabbit
      - postgres

testing.postgresql.Postgresql(copy_data_from='data/postgres:/var/lib/postgresql/data')

【问题讨论】:

    标签: python postgresql docker sqlalchemy python-unittest


    【解决方案1】:

    您需要以postgresql 用户而不是root 身份运行此命令,因此您可以尝试使用以下命令运行命令:

    runuser -l  postgres -c 'command'    
    

    su -c "command" postgres
    

    或将USER postgres 添加到您的Dockerfile

    并检查要求:

    Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
    pg8000 1.10
    

    更新

    要使copy_data_from 工作,您应该首先生成文件夹:

    FROM python:slim-jessie
    ADD requirements.txt /app/requirements.txt
    ADD . /app/
    WORKDIR /app/
    RUN pip install -r requirements.txt
    RUN /PATH/TO/initdb -D myData -U postgres
    

    然后添加:

    pg = testing.postgresql.Postgresql(copy_data_from='myData')
    

    【讨论】:

    • 但是我已经有了 POSTGRES_USER=postgres 并在 docker-compose 的 postgres 环境中设置了它的密码,我需要在我的 dockerfile 中再次添加它吗?
    • 所以现在我在我当前的“worker”(主项目容器)而不是“postgres”容器中运行“python -m unittest discover”。我需要将其更改为 postgres 容器吗?
    • 我将“USER postgres”添加到与工作容器一起运行的 dockerfile 中,然后重新启动我的容器,但它仍然有错误
    • 重启不会帮助你重建它
    • “无法启动服务人员:无法找到用户 postgres:passwd 文件中没有匹配的条目”当我重建它时,我还需要 docker 文件中的其他内容吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    相关资源
    最近更新 更多