FATAL: role 'username' does not exist

Postgres Role Does Not Exist Error Fix

You're trying to connect to Postgres with a user that doesn't exist in the database. The fix is creating the role or using an existing one like postgres.

Quick answer

Run sudo -u postgres psql -c "CREATE ROLE yourusername LOGIN SUPERUSER;" on Linux, or create the role via pgAdmin on Windows. Then reconnect with your username.

Why this happens

When you install PostgreSQL, it creates a default superuser called postgres. Your current system user — let's say john — isn't a database role. So when you run psql -U john, Postgres looks for a role named john and doesn't find it. That's when you get the FATAL: role 'john' does not exist error.

I see this all the time on fresh installs or when someone migrates from a different database system. It's not a bug — Postgres just works differently. It doesn't automatically create a role for every system user.

Fix it in 3 steps

Step 1: Check if the role exists

Connect as the postgres user and list all roles:

sudo -u postgres psql -c '\du'

On Windows, use:

psql -U postgres -c '\du'

You'll see a table of roles. If your username isn't there, move to step 2.

Step 2: Create the missing role

Still as the postgres user, run this:

sudo -u postgres psql -c "CREATE ROLE yourusername LOGIN SUPERUSER PASSWORD 'yourpassword';"

Replace yourusername with your actual system username. The password is optional but recommended for security. On Windows, do the same from the command line with psql -U postgres -c "CREATE ROLE ...".

Step 3: Test the connection

Try connecting with your new role:

psql -U yourusername -d postgres

If it works, you're good. If you get a password prompt, use the password you set in step 2.

Alternative fixes if the main one fails

Use the postgres user directly

If you don't want to create a new role, just always connect as postgres. It's the superuser and works everywhere. The command is:

psql -U postgres

On Linux, you might need sudo -u postgres psql instead. This is the lazy fix, but it works.

Create a role through pgAdmin

If you prefer GUI tools:

  1. Open pgAdmin and connect with the postgres user.
  2. Right-click on Login/Group Roles in the browser tree.
  3. Select Create > Login/Group Role.
  4. Give it a name, set a password, check Superuser, and save.

This creates the role without typing SQL commands. I use this on client machines where people hate the terminal.

Reset the pg_hba.conf to trust authentication

Sometimes the error is really about authentication, not a missing role. If you're sure the role exists, check pg_hba.conf. On Linux, it's usually in /etc/postgresql/*/main/pg_hba.conf. Change the line:

local   all   all   md5

to:

local   all   all   trust

Then restart Postgres: sudo systemctl restart postgresql. This lets you connect without a password. It's a hack, not a long-term fix.

How to prevent this error

When you install PostgreSQL, create a role for your system user right away. Add this to your install notes:

sudo -u postgres createuser --superuser $USER

On Ubuntu/Debian, the createuser command does exactly what the SQL CREATE ROLE does. It's one line and saves you the headache later.

Also, don't use the same username for everything. If you have multiple apps, create separate roles for each. It keeps permissions clean and you'll never confuse which role belongs to which app.

I've seen this error pop up after a Postgres upgrade too. The upgrade script sometimes drops roles that were created manually. So after an upgrade, always run \du to verify your roles are still there.

Related Errors in Database Errors
Cannot open database requested by the login. The login failed. Fix 'Cannot open database' error in SQL Server Fix MongoDB 'Operation exceeded time limit' on aggregations 0XC00000E4 STATUS_INTERNAL_DB_CORRUPTION (0XC00000E4) Fix Guide 1215 MySQL 1215: Foreign Key Constraint Fix Steps That Work

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.