简体   繁体   English

从 Python 容器连接到 postgres 时出错

[英]Error when connecting to postgres from a Python container

I'm trying to set up two communicating containers as different services我正在尝试将两个通信容器设置为不同的服务

  1. PostgreSQL for holding the data用于保存数据的 PostgreSQL
  2. Python with Jupyter notebook for querying the database带有 Jupyter notebook 的 Python 用于查询数据库

Here's my Dockerfile这是我的Dockerfile

FROM python:3.8.8-slim-buster

RUN mkdir /project
WORKDIR /project
COPY . /project/

RUN apt-get update && \
    apt-get install --no-install-recommends -y \
    build-essential \
    bash

RUN pip install --upgrade --no-use-pep517 \
    pip \
    setuptools \
    wheel \
    jupyterlab \
    ipython-sql \
    psycopg2-binary

EXPOSE 8888

and my docker-compose.yml和我docker-compose.yml

version: '3'

services:
  db:
    image: postgres:12.1-alpine
    environment:
      - POSTGRES_DB=ps_db
      - POSTGRES_USER=ps_user
      - POSTGRES_PASSWORD=ps_pass
    ports:
      - "5432:5432"
  web:
    build: 
      context: .
    command: 
      jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
    volumes:
      - .:/project
    ports:
      - "8888:8888"
    depends_on:
      - db

Although I'm pretty sure my settings are fine, whenever I run虽然我很确定我的设置很好,但每当我运行

import psycopg2
conn = psycopg2.connect(database="ps_db", user="ps_user", password="ps_pass", host="0.0.0.0", port=5432)

I get this error我收到这个错误

OperationalError: could not connect to server: Connection refused
    Is the server running on host "0.0.0.0" and accepting
    TCP/IP connections on port 5432?

You need to set the 0.0.0.0 IP to 'db', because the postgres runs in another container called 'db', this is the name of your service.您需要将 0.0.0.0 IP 设置为 'db',因为 postgres 运行在另一个名为 'db' 的容器中,这是您的服务的名称。 So your conn will look like this:所以你的 conn 看起来像这样:

conn = psycopg2.connect(database="ps_db", user="ps_user", password="ps_pass", host="db", port=5432)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到Docker容器中的Postgres时出现身份验证错误 - Authentication error when connecting to Postgres in a Docker container 将 docker 服务连接到 docker 数据库容器时出现 Postgres ENOTFOUND 错误 - Postgres ENOTFOUND error when connecting docker services to a docker database container 从Docker容器连接到Postgres - Connecting to postgres from a docker container 连接到Postgres时出现rgdal错误 - rgdal error when connecting to postgres 从Docker容器连接到本地postgres数据库 - Connecting to the local postgres database from the Docker container 从typeorm docker容器连接postgres - Connecting postgres from typeorm docker container 连接 pgadmin 容器和 postgres 容器 - connecting pgadmin container and postgres container Docker:当使用容器名称从一个容器连接到另一个容器时,如何修复“无法将主机名”postgres“转换为地址”? - Docker: How to fix “could not translate host name ”postgres“ to address” when connecting from one container to another using container name? 连接到Heroku Postgres数据库时出现CertificateUnknown错误 - CertificateUnknown error when connecting to Heroku Postgres database 从UWP应用程序连接到Postgres数据库时出错 - Error connecting to Postgres database from UWP application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM