PostgreSQL error: Fatal: role "username" does not exist

Viewed 1129255

I'm setting up my PostgreSQL 9.1. I can't do anything with PostgreSQL: can't createdb, can't createuser; all operations return the error message

Fatal: role h9uest does not exist

h9uest is my account name, and I sudo apt-get install PostgreSQL 9.1 under this account.
Similar error persists for the root account.

19 Answers
psql postgres

postgres=# CREATE ROLE username superuser;
postgres=# ALTER ROLE username WITH LOGIN;

For version Postgres 9.5 use following comand:

psql -h localhost -U postgres

Hope this will help.

For Windows users : psql -U postgres

You should see then the command-line interface to PostgreSQL: postgres=#

I did a healthcheck with docker-compose.

healthcheck:
  test: ['CMD-SHELL', 'pg_isready']
  interval: 5s
  timeout: 5s
  retries: 5

If you also have that change the user:

healthcheck:
  test: ['CMD-SHELL', 'pg_isready -U postgres'] # <<<---
  interval: 5s
  timeout: 5s
  retries: 5

Manually creating a DB cluster solved it in my case.

For some reason, when I installed postgres, the "initial DB" wasn't created. Executing initdb did the trick for me.

This solution is provided in the PostgreSQL Wiki - First steps:

initdb

Typically installing postgres to your OS creates an "initial DB" and starts the postgres server daemon running. If not then you'll need to run initdb

dump and restore with --no-owner --no-privileges flags

e.g.

dump - pg_dump --no-owner --no-privileges --format=c --dbname=postgres://userpass:username@postgres:5432/schemaname > /tmp/full.dump

restore - pg_restore --no-owner --no-privileges --format=c --dbname=postgres://userpass:username@postgres:5432/schemaname /tmp/full.dump

sudo -u postgres createuser --superuser $USER

sudo -u postgres createdb $USER

This should definitely work for you.

for those who using docker and correctly followed the instructions from official doc, if you still met this problem, RESTART windows and try again.

Follow these steps to get postgres working.

  1. In your terminal, locate the Application Support folder with the following command. /Users/[name of the user]/library/application support
  2. Delete the application, Postgres.
  3. Reinstall the app and it should work just fine.

Something as simple as changing port from 5432 to 5433 worked for me.

Related