sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server

The error says:- sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at “postgres” (172.18.0.2), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

My yml file is:-

version: '3.8'

services:
  postgres:
    image: ankane/pgvector
    container_name: my_postgres_db
    restart: always
    expose:
      - "5432"
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=
      - POSTGRES_DB=vectordb
      - POSTGRES_HOST_AUTH_METHOD=trust
    volumes:
      - ./postgresql.conf:/etc/postgresql/postgresql.conf
      - postgres_data:/var/lib/postgresql/data

    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres", "-d", "vectordb"]
      interval: 30s
      retries: 3
      start_period: 30s
      timeout: 10s

  web:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - postgres
    command: bash -c "python database.py && ./wait-for-it.sh postgres:5432 --timeout=120 --strict -- uvicorn main:app --host 0.0.0.0 --port 8000"
    env_file:
      - .env


volumes:
  postgres_data:

My database.py for postgres fast api is like this:-

TESTING = os.getenv('TESTING', 'False') == 'True'

DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://postgres@postgres:5432/vectordb')

Base = declarative_base()

SQLALCHEMY_DATABASE_URL = DATABASE_URL if not TESTING else 'sqlite:///./test.db'

engine = create_engine(SQLALCHEMY_DATABASE_URL, echo=True)

sessionLocal = sessionmaker(autocommit = False, autoflush = False, bind = engine)

Please, format your post according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

```
echo "I am a code."
echo "An athletic one, and I wanna run."
```

After fixing your post, please send a new comment so people are notified about the fixed content.


I edited the file according to the guide. You can see the code block.

Thank you for the formatting. It seems the container name is correctly used to connect to the database, but something must be wrong in the database container. I don’t know that image and there is nothing on Docker Hub about it, except that it is an archived image, so you shouldn’t even use it.

https://hub.docker.com/r/ankane/pgvector

Try the connection with an official postgres image. If that works, the problem is with the archived image and you should look for a supported version for what you want.

https://hub.docker.com/_/postgres

Hi, i changed it to postgres:15. But I am using Vector in my db and I would need vector extension for that. The new error:-

I suggested using the official image only for testing the connection. If the connection works with that, you had an application issue with the other image and you could contact the maintainer, if the image were not archived and you could find any contact info.

Okay I solved the issue of vector. But the main issue still persists even after using postgres:15 image.

services:
  postgres:
    image: postgres:15
    container_name: my_postgres_db
    restart: always
    expose:
      - "5432"
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=
      - POSTGRES_DB=vectordb
      - POSTGRES_HOST_AUTH_METHOD=trust
    volumes:
      - ./postgresql.conf:/etc/postgresql/postgresql.conf
      - postgres_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres", "-d", "vectordb"]
      interval: 30s
      retries: 3
      start_period: 30s
      timeout: 10s

Then check if the postgres container is running. If it worked without modification, the modification broke it. If you “fixed” the vector issue by adding the same files that the other image had, you could end up with the same error easily. You only showed the web container logs, but it is time to check the database logs.

And please, don’t share screenshots of texts. Fortunately these images were easily readable (at least on my screen), but some users don’t even check screenshots when it shows texts as it is hard to read and impossible to quote. You can share logs the same way as you formatted your first message, so special characters will not be interpreted by the forum.

If you are using Docker Desktop, that allows copying logs as well.