testing.postgresql command not found: initdb inside docker

2024/10/18 13:01:02

Hi i'm trying to make a unittest with postgresql database that use sqlalchemy and alembic

Also im running it on docker postgresql

I'm following the docs of testing.postgresql(docs) to set up a temporary postgresql instance handle database testing and wrote the the following test:

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')

The crawl_bds_obj() basically save information from an URL and save it to the database using session.commit() and return the type data

When i tried to run the test it return the following error:

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

In the docs it said that "testing.postgresql.Postgresql executes initdb and postgres on instantiation. On deleting Postgresql object, it terminates PostgreSQL instance and removes temporary directory."

So why am i getting initdb error when i already installed testing.postgresql and had postgresql running on my docker?

EDIT:

I aslo had set my data path but it still return the same error

dockerfile:

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

docker-compose:

  postgres:image: postgresrestart: alwaysenvironment:- 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/datapgadmin:image: dpage/pgadmin4environment:PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}volumes:- ./data/pgadmin:/root/.pgadminports:- "${PGADMIN_PORT}:80"logging:driver: nonerestart: unless-stoppedworker:build:context: .dockerfile: Dockerfilecommand: "watchmedo auto-restart --recursive -p '*.py'"environment:- C_FORCE_ROOT=1volumes:- .:/applinks:- rabbitdepends_on:- rabbit- postgres

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

Answer

you need to run this command as postgresql user not root, so you may try to run your commands using:

runuser -l  postgres -c 'command'    

or

su -c "command" postgres

or add USER postgres to your Dockerfile

and check the requirments:

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

UPDATE

To make copy_data_from works you should generate the folder first:

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

and then add this:

pg = testing.postgresql.Postgresql(copy_data_from='myData')
https://en.xdnf.cn/q/72715.html

Related Q&A

Recommended approach for loading CouchDB design documents in Python?

Im very new to couch, but Im trying to use it on a new Python project, and Id like to use python to write the design documents (views), also. Ive already configured Couch to use the couchpy view server…

Error when import matplotlib.pyplot as plt

I did not have any problem to use "plt", but it suddenly shows an error message and does not work, when I import it. Please see the below. >>> import matplotlib >>> import m…

Python NtQueryDirectoryFile (File information structure)

Ive written a simple (test) script to list files in a selected directory. Not using FindFirstFile; only native API. When I execute the script and watch, Win32API monitor tells me STATUS_SUCCESS. My Fil…

returning A DNS record in dnspython

I am using dnspython to get the A record and return the result (IP address for a given domain).I have this simple testing python script:import dns.resolverdef resolveDNS():domain = "google.com&quo…

IO completion port key confusion

Im writing an IO completion port based server (source code here) using the Windows DLL API in Python using the ctypes module. But this is a pretty direct usage of the API and this question is directed…

PySpark reversing StringIndexer in nested array

Im using PySpark to do collaborative filtering using ALS. My original user and item ids are strings, so I used StringIndexer to convert them to numeric indices (PySparks ALS model obliges us to do so).…

Numba np.convolve really slow

Im trying to speed up a piece of code convolving a 1D array (filter) over each column of a 2D array. Somehow, when I run it with numbas njit, I get a 7x slow down. My thoughts:Maybe column indexing is …

Python: Retrieving only POP3 message text, no headers

Im trying to make a Python program that retrieves only the body text of an email without passing headers or any other parameters. Im not sure how to go about this.The goal is to be able to send basic c…

Getting text between xml tags with minidom [duplicate]

This question already has answers here:Getting text values from XML in Python(2 answers)Closed 9 years ago.I have this sample xml document snippet<root><foo>bar</foo><foo>baz<…

OpenCV Error: Unknown error code -49 in Python

I am trying to learn face detection in python-3.6 using cv2.I follow the src given in a book.I have already installed opencv-python(3.2.0) by pip.xml and .jpg files are all in the same path with python…