Skip to content

How to Fix PostgreSQL Role Does Not Exist

DodaTech 1 min read

In this tutorial, you'll learn about How to Fix PostgreSQL Role Does Not Exist. We cover key concepts, practical examples, and best practices.

The Problem

You connect to PostgreSQL and get:

psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "myuser" does not exist

Or:

FATAL: role "myuser" does not exist

PostgreSQL uses roles for authentication. The role you are trying to connect with has not been created, or you are using the wrong username.

Quick Fix

Step 1: Connect as the postgres superuser

sudo -u postgres psql

Step 2: Create the missing role

Inside psql:

CREATE ROLE myuser WITH LOGIN PASSWORD 'yourpassword';

Or with additional privileges:

CREATE ROLE myuser WITH LOGIN SUPERUSER CREATEDB PASSWORD 'yourpassword';

Step 3: Create a database for the role

CREATE DATABASE mydb OWNER myuser;

Step 4: Test the connection

psql -U myuser -d mydb -h localhost

Expected output:

Password for user myuser:
psql (16.2)
Type "help" for help.

mydb=>

Step 5: Exit psql

\q

Alternative Solutions

Use createuser command-line tool:

sudo -u postgres createuser --interactive myuser

Or use peer authentication by matching the system username:

# If your system user is 'john', connect as 'john'
psql

This works if pg_hba.conf has a local line with peer authentication.

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Prevention

  • Create roles and databases together during project setup.
  • Document the database username and password for your team.
  • Use a consistent naming convention: projectname_user.
  • Automate database setup with shell scripts or Makefiles.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro