0

I'm deploying to AWS Linux 2023 and my postgresql database in on aws RDS. I've installed psql and checked if db is accessible in my instance. I've also checked that the environmental variables are fetching exactly as expected in my settings.py. and I even ssh and applied the migrations myself. but I keep getting the following issue:

[stderr] raise dj_exc_value.with_traceback(traceback) from exc_value
[stderr] File "/home/ec2-user/.local/share/virtualenvs/app-UPB06Em1/lib/python3.13/site-packages/django/db/backends/base/base.py", line 279, in ensure_connection
[stderr] self.connect()
[stderr] ~~~~~~~~~~~~^^
[stderr] File "/home/ec2-user/.local/share/virtualenvs/app-UPB06Em1/lib/python3.13/site-packages/django/utils/asyncio.py", line 26, in inner
[stderr] return func(*args, **kwargs)
[stderr] File "/home/ec2-user/.local/share/virtualenvs/app-UPB06Em1/lib/python3.13/site-packages/django/db/backends/base/base.py", line 256, in connect
[stderr] self.connection = self.get_new_connection(conn_params)
[stderr] ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
[stderr] File "/home/ec2-user/.local/share/virtualenvs/app-UPB06Em1/lib/python3.13/site-packages/django/utils/asyncio.py", line 26, in inner
[stderr] return func(*args, **kwargs)
[stderr] File "/home/ec2-user/.local/share/virtualenvs/app-UPB06Em1/lib/python3.13/site-packages/django/db/backends/postgresql/base.py", line 332, in get_new_connection
[stderr] connection = self.Database.connect(**conn_params)
[stderr] File "/home/ec2-user/.local/share/virtualenvs/app-UPB06Em1/lib64/python3.13/site-packages/psycopg2/__init__.py", line 122, in connect
[stderr] conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
[stderr]django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
[stderr] Is the server running locally and accepting connections on that socket?
[stderr]
[stderr]2025-10-19 04:29:22,032 INFO Starting server at tcp:port=8000:interface=127.0.0.1
[stderr]2025-10-19 04:29:22,032 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
[stderr]2025-10-19 04:29:22,033 INFO Configuring endpoint tcp:port=8000:interface=127.0.0.1
[stderr]2025-10-19 04:29:22,033 INFO Listening on TCP address 127.0.0.1:8000

settings.py:

...
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": DB_NAME,
        "USER": DB_USER,
        "PASSWORD": DB_PASSWORD,
        "HOST": DB_HOST,
        "PORT": DB_PORT,
    }
}
...

any possible ideas why this issue is happening?

Edit: I'm using daphne to run the app and codedeploy to deploy the app. also note that the migration didn't get applied when code deploy run my start_app.sh

  • python3.13
  • pipenv

my start_app.sh:

cd /home/ec2-user/app


if [ ! -f /etc/ssl/private/privkey.pem ]; then
    sudo mkdir -p /etc/ssl/private /etc/ssl/certs
    sudo openssl req -x509 -nodes -days 365 \
        -newkey rsa:2048 \
        -keyout /etc/ssl/private/privkey.pem \
        -out /etc/ssl/certs/fullchain.pem \
        -subj "/C=US/ST=State/L=City/O=MyOrg/OU=MyDept/CN=localhost"
    sudo chmod 600 /etc/ssl/private/privkey.pem
    sudo chmod 644 /etc/ssl/certs/fullchain.pem
fi

python3.13 -m pipenv install --deploy --ignore-pipfile
python3.13 -m pipenv run python manage.py migrate

pkill -f daphne || true

python3.13 -m pipenv run daphne -b 127.0.0.1 -p 8000 core.asgi:application &

DAPHNE_PID=$!

# Check if the process is still running
sleep 10
if ! kill -0 "$DAPHNE_PID" 2>/dev/null; then
    echo "Daphne failed to start — exiting."
    exit 1
fi

sudo systemctl restart nginx

1 Answer 1

0

The error means your Django app is trying to connect to PostgreSQL via a local UNIX socket instead of your AWS RDS instance. Specifically, this part of the traceback shows it:

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed:
No such file or directory

That happens when Django thinks the database is local because either:

  1. DB_HOST is empty or incorrect, or
  2. The environment variables are not loaded correctly when daphne starts.

To fix it

  1. Check the actual values Django gets Run this inside your instance:

    python3.13 manage.py shell
    

    Then:

    from django.conf import settings
    print(settings.DATABASES)
    

    Ensure "HOST" shows your RDS endpoint (like mydb.xxxxxx.us-east-1.rds.amazonaws.com), not empty or "localhost".


  1. Fix environment loading in CodeDeploy

    In your start_app.sh, migrations and Daphne may not see environment variables if you only export them in another context.

    Make sure you export them in the script or load them from an .env file:

    export DB_NAME=mydb
    export DB_USER=myuser
    export DB_PASSWORD=mypassword
    export DB_HOST=mydb.xxxxxx.us-east-1.rds.amazonaws.com
    export DB_PORT=5432
    

    Or use:

    source /home/ec2-user/app/.env
    

    Then start Django.


  1. Test direct connection

    psql -h $DB_HOST -U $DB_USER -d $DB_NAME
    

    If this works but Django still fails, the problem is environment visibility.


  1. Don’t use 127.0.0.1 in Daphne (-b 127.0.0.1) unless nginx is reverse proxying to it. It doesn’t affect the DB issue but can hide errors when debugging.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.