![](/img/trans.png)
[英]In testing.postgresql, cannot find initdb command inside Docker
[英]testing.postgresql command not found: initdb inside docker
嗨,我正在嘗試使用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錯誤?
編輯:
我也已經設置了我的數據路徑,但它仍然返回相同的錯誤
dockerfile:
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 ='數據/ postgres:/ var / lib / postgresql /數據')
您需要以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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.