如何使用 psycopg2 修复我的 docker compose 错误

kil*_*ale 3 psycopg2 docker

我正在配置 Docker 以使用 Django,但我在运行 Docker 时遇到问题,这可能是 Psycopg 问题,我应该如何解决这个问题?

我的码头工人错误:

Uninstalling psycopg2-2.8.4:
      Successfully uninstalled psycopg2-2.8.4
    Running setup.py install for psycopg2: started
    Running setup.py install for psycopg2: finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-71k484j0/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-71k484j0/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-klbk9cly/install-record.txt --single-version-externally-managed --compile
         cwd: /tmp/pip-install-71k484j0/psycopg2/
    Complete output (72 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.8
    creating build/lib.linux-x86_64-3.8/psycopg2
    copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/extras.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/psycopg1.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/pool.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_range.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/extensions.py -> build/lib.linux-x86_64-3.8/psycopg2
    creating build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_bug_gc.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_lobject.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_sql.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/dbapi20_tpc.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_quote.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_psycopg2_dbapi20.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/testconfig.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_dates.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_notify.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_errcodes.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_connection.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_cancel.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_replication.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_bugX000.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_fast_executemany.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_copy.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_green.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_types_basic.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_cursor.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_async_keyword.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_types_extras.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_with.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/testutils.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_extras_dictcursor.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_module.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/dbapi20.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_transaction.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    copying tests/test_async.py -> build/lib.linux-x86_64-3.8/psycopg2/tests
    Skipping optional fixer: buffer
    Skipping optional fixer: idioms
    Skipping optional fixer: set_literal
    Skipping optional fixer: ws_comma
    running build_ext
    building 'psycopg2._psycopg' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/psycopg
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION=2.7.7 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=110005 -DHAVE_LO64=1 -I/usr/local/include/python3.8 -I. -I/usr/include/postgresql -I/usr/include/postgresql/11/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.8/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    psycopg/psycopgmodule.c: In function ‘psyco_is_main_interp’:
    psycopg/psycopgmodule.c:689:18: error: dereferencing pointer to incomplete type ‘PyInterpreterState’ {aka ‘struct _is’}
         while (interp->next)
                      ^~

    It appears you are missing some prerequisite to build the package from source.

    You may install a binary package by installing 'psycopg2-binary' from PyPI.
    If you want to install psycopg2 from source, please install the packages
    required for the build and try again.

    For further information please check the 'doc/src/install.rst' file (also at
    <http://initd.org/psycopg/docs/install.html>).

    error: command 'gcc' failed with exit status 1
    ----------------------------------------

Run Code Online (Sandbox Code Playgroud)

文件:

FROM python:3
RUN export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
RUN pip3 install psycopg2-binary
RUN pip3 install psycopg2
ENV PYTHONUNBUFFERED 0
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
Run Code Online (Sandbox Code Playgroud)

我尝试安装 Psycopg2-Binary,但它仍然存在

3 天以来,我一直遇到与 Docker 相关的安装问题。

有人有什么好主意吗?

zno*_*ead 5

更新到 psycopg2.8 或更高版本以支持最新的 python https://github.com/psycopg/psycopg2/issues/854


gho*_*vat 1

在你的docker文件中请运行,你需要安装一些额外的依赖项,python开发,psql contrib等

sudo apt update
sudo apt-get install  postgresql-contrib
sudo apt-get install libpq-dev gcc # this is required as psycopg2 uses pg_config
sudo apt-get install python3-dev
Run Code Online (Sandbox Code Playgroud)

当然,apt 安装应该在 pip 安装之前进行。

在您运行的脚本中,pip install ....请将其更改为pip3