Question

Dockerized Python Web Application Can't Connect to PostgreSQL Database: Connection Refused Error

I am trying to set up a Docker environment for my Python web application. I have configured Nginx as a reverse proxy, Gunicorn as a WSGI HTTP server, and I’m using Redis for caching and PostgreSQL as my database. I’ve also tried to implement load balancing with Nginx and I’m using Docker Compose for orchestrating all these services.

Everything seemed to be configured correctly, but when I try to run the application, I’m experiencing an issue where my application isn’t communicating with the PostgreSQL database. The application logs are indicating a connection error. Here’s the error I’m seeing:

psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "db" (172.20.0.2) and accepting TCP/IP connections on port 5432?

I’ve checked my Docker Compose file and everything seems to be in order. Here’s a part of my docker-compose.yml:

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword

  web:
    build: .
    command: gunicorn myapp:app -c ./gunicorn_config.py
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

And this is how I’m trying to connect to the database in my Python application:

import psycopg2
conn = psycopg2.connect(
    dbname="mydb",
    user="myuser",
    password="mypassword",
    host="db"
)

I can’t figure out what I’m doing wrong. Any ideas why the application is unable to connect to the PostgreSQL database?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 11, 2023
Accepted Answer

Hello there,

There could be a couple of issues here, but I’ll start with the most common one when dealing with Docker and databases and you can let me know how it goes.

The problem is likely that your web application is starting before your PostgreSQL database is fully ready to accept connections. Even though you’ve specified depends_on in your docker-compose.yml, this only ensures that the db service starts before the web service. It doesn’t wait for the db service to be “ready” before starting web.

To solve this issue, you can use a simple script that checks the database connection before starting your application. Here’s a simple Python script that does exactly that:

import time
import psycopg2
from psycopg2 import OperationalError

def create_conn():
    conn = None
    while not conn:
        try:
            conn = psycopg2.connect(
                dbname="mydb",
                user="myuser",
                password="mypassword",
                host="db"
            )
            print("Database connection successful")
        except OperationalError as e:
            print(e)
            time.sleep(5)
    return conn

conn = create_conn()

In this script, we’re using a while loop to try to establish a connection to the database. If it can’t connect, it will wait for 5 seconds before trying again. Once the connection is established, it will break the loop and proceed.

You can adjust this script to your needs, for instance, by limiting the number of connection attempts or increasing the sleep time.

Another approach would be to use a tool like wait-for-it or dockerize which can pause the execution of your application until the db service is ready to accept connections.

If this doesn’t solve your problem, let me know. It might be a more specific issue with your configuration or environment.

Hope that helps!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.