PostgreSQL error: role "myapp" does not exist when my web app connects
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
App logs show 'role "myapp" does not exist' at connection time; connection attempts do not proceed to authentication or query phase.
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
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
- 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.
- 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.
- 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.
- 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
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
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?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags