Databases

PostgreSQL error: role "myapp" does not exist when my web app connects

Environment

Environment

PostgreSQL instance(s) reachable via host/port; application runs in Docker container using DATABASE_URL = postgres://myapp:secret@db:5432/mydb; there may be multiple PostgreSQL clusters (test/staging/production) and connection pooling (PgBouncer) in front.

Symptoms

Symptoms

App logs show 'role "myapp" does not exist' at connection time; connection attempts do not proceed to authentication or query phase.

Observations

Observations

psql -h db -U myapp -d mydb from local workstation fails with same error; psql as postgres user connects locally and  select rolname from pg_roles shows pre-existing roles not including 'myapp'. There is a PgBouncer service between app and DB in some environment variants.

Already attempted

Already attempted

Inspected connection string for typos; confirmed password and host values; tried connecting with psql as different roles; checked Docker environment variables for accidental override.

Diagnostic steps

Diagnostic steps

  1. From the application host, run psql -h <host> -p <port> -U <user> -d <db> --no-password (or PGPASSWORD set) and capture the exact server banner: SELECT version(), pg_settings.setting FROM pg_settings WHERE name='data_directory';
  • What the result means: If you can connect as a superuser, these values identify the specific cluster. If psql fails with same error, you're likely hitting the same server the app hits and the role is missing there.
  1. Resolve the host the app uses: docker exec into container and run getent hosts $(echo $DATABASE_HOST) or dig/A record; also check if a local PgBouncer is bound to same port (ss -ltnp | grep :5432).
  • What the result means: If DNS resolves to a different IP than intended or a local proxy is bound, you are talking to a proxy/other host rather than the expected Postgres server.
  1. If a pooling proxy exists, inspect its config (user mapping, auth_type) and check its logs for connection attempts from the app.
  • What the result means: PgBouncer can reject/translate users; logs will show whether it handed off auth to the server or rejected before. This determines where the role must exist.
  1. As a safe, reversible action, create the role with minimal privileges: CREATE ROLE myapp LOGIN PASSWORD 'temporary'; then test connection; drop role after tests if unintended.
  • What the result means: If creating the role lets the app connect, the problem was missing role; if not, the server the app reaches is still incorrect or password/auth mismatched.
Confirmed fixes

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

Question

How can I reliably determine which Postgres instance and cluster the app is hitting and safely create or restore the missing role without disrupting other tenants?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Client connecting to unintended PostgreSQL instance/cluster (wrong host, Docker network misrouting, or service discovery error) 9%
Role truly missing in target cluster; role not created during provisioning or was dropped 7%
PgBouncer or connection pool in between performing auth or mapping user differently 6%

Summary

'role does not exist' indicates the server that answered the TCP connection lacks a role matching the username supplied by the client. Causes include connecting to the wrong cluster, missing role in the intended cluster, or being routed through a pooling/proxy layer that rewrites/authenticates differently. Diagnostics should confirm the server identity (version, data_directory, system identifier), the connection path (DNS/IP and any PgBouncer), and then create the role in the correct cluster or fix client configuration. Creating roles is reversible (DROP ROLE) but changes must be coordinated if multiple apps share the cluster.

Suggested Fixes

Diagnostic step 1
From the application host, run psql -h <host> -p <port> -U <user> -d <db> --no-password (or PGPASSWORD set) and capture the exact server banner: SELECT version(), pg_settings.setting FROM pg_settings WHERE name='data_directory'; Result meaning: If you can connect as a superuser, these values identify the specific cluster. If psql fails with same error, you're likely hitting the same server the app hits and the role is missing there.
Diagnostic step 2
Resolve the host the app uses: docker exec into container and run getent hosts $(echo $DATABASE_HOST) or dig/A record; also check if a local PgBouncer is bound to same port (ss -ltnp | grep :5432). Result meaning: If DNS resolves to a different IP than intended or a local proxy is bound, you are talking to a proxy/other host rather than the expected Postgres server.
Diagnostic step 3
If a pooling proxy exists, inspect its config (user mapping, auth_type) and check its logs for connection attempts from the app. Result meaning: PgBouncer can reject/translate users; logs will show whether it handed off auth to the server or rejected before. This determines where the role must exist.

Continue troubleshooting

Matched from the title, description and tags