chevron_left
1,680 points
10 5 2

Psql fatal role root does not exist

The Error: Psql fatal role root does not exist will occur when a user will try to log in to a PostgreSQL database without and required parameters. It may be because the users who have experience with MySQL/MariaDB when they tried to log in without any username and password, the service allows them to connect with the default username and password. The simple solution is the user should use proper user credentials or create a new user before connecting to the database.

 What is the error Description  #

You can get the following problem when using Postgres for the first time psql: FATAL role root does not exist or psql: FATAL: database Postgres does not exist. This can happen, in particular, when you run psql from the command line without any arguments. The problem is that psql believes you want to access a database with the same name as your user name if you don't supply any arguments.

PostgreSQL will try to log you into a database with the same name as your user account when you log in using a user other than the Postgres user, which is the database's default user. This indicates that the psqlstrong>command will try to log you into the database root if you attempt to use it as root. The database ‘Hassan’ will be searched for if a user tries to log in while signing in as ‘Hassan’, and so on.

The message "Psql fatal role root does not exist" is displayed by PostgreSQL when it is unable to locate this database.

Warning: No other default Linux or Unix system user name should be used to log in.

The command mysql -u root -p from any account may be used to access the database if you have previously used MySQL/MariaDB. PostgreSQL, however, has a distinct security approach.

When PostgreSQL is installed, the system automatically creates a user account for PostgreSQL. Without a password, the Postgres user can access PostgreSQL. Nobody else has access to PostgreSQL.

What brings the error  #

The incorrect configuration of the System login and Database Username is to blame for this. The majority of Linux users try to log in to PostgreSQL as the root user. However, this is incorrect. To create the initial user account, you must log in as the operating system user that PostgreSQL was installed as. Typically, Postgres is used as both the default database and user name. Once PostgreSQL is installed on Linux, the Postgres user is the default PostgreSQL's super user and should have a password configured.

The Solution  #

A newly launched system always has one preconfigured role to bootstrap the database system. This role is always a "superuser," and by default (unless changed when running initdb, it will have the same name as the user who initialized the database cluster in the operating system. This role will typically go with the name Postgres. You must initially connect as this initial role in order to create additional roles.

You won't be able to utilize the command line as any other user to access the database. This indicates that you must use the command to switch to that user account before using PostgreSQL.

If you are a Linux or Unix user then use the below command

su - Postgres

After that, you may use the following command to log into the PostgreSQL client:

psql

And create a password for the Default Postgres user:

sudo passwd Postgres

Using "Postgres", and create a new role:

Adding the role "dbinfant." Set the password using -p, and allow database creation with -d.

sudo -u postgres createuser dbinfant -d -P

If you are using a macOS then follow these steps to solve the issue

test do
  system "#{bin}/initdb", "pgdata"
end

The PostgreSQL command initdb is used to launch the database. There isn't any further information regarding the user creation besides the pgdata option. I, therefore, thought that initdb was responsible for creating default users.

I explored the inner workings of the initdb source because I was unsatisfied. I discovered that the current OS username is utilized by default if no username (—username operator) is provided. The get id method accomplishes this.

effective_user = get_id();
if (!username)
            username = effective_user;

Naturally, as you could anticipate, Postgres is not my username.

With this knowledge at hand, creating the Postgres DB user just required entering my username into the createuser command.

$ createuser -s postgres -u 

However, a query is still open. Why do the procedures psql and createuser presume a Postgres user when initdb expects the os user as the default root user? There appears to be a huge hole in this.

When I looked at the initdb documentation's —username operator, the following was what I discovered:

Information: chooses the superuser's username for the database. The name of the actual user who is running initdb is used by default. The superuser's name is not that crucial, but even if the operating system user has a different name, one may decide to stick with the traditional name Postgres.

Window users simply can create a new user and set the role for the user, type the below command in the command prompt

psql -U Postgres

to enter into Postgres user and create a new user and set the role for that user

The Conclusion  #

We have discussed the details of the error: psql fatal role root does not exist. The error occurs due to misconfiguration or when the user tries to log in without any user credentials and thinks according to the previous experience that the service will allow them to log in. But the security system of PostgreSQL is entirely different and it does not allow connection to the database in such a way. First of all, logging in with the default user and then creating a new user and setting a role for that user is the proper solution.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

How to fix target class controller does not exist in laravel 8 mgndevftart - Sep 4
Terminating Connection Due To Administrator Command zhteja - Sep 16
“Thread 1: fatal error: unexpectedly found nil while unwrapping an optional value” - what does it mean? zhteja - Aug 22
Indentationerror: unindent does not match any outer indentation level Ankur Ranpariya - Sep 19
How to say String does not equal in java AnkurRanpariyav - Aug 7
TypeError: 'str' object does not support item assignment zhteja - Aug 1
Str' object does not support item assignment zhteja - Jul 30
Response for preflight does not have HTTP ok status. zhteja - Jul 26
Error does not name a type C++ cafe-coder45 - Jun 2
The Markup in the Document Preceding the Root Element Must be Well-Formed. zhteja - Jul 5
TypeError: 'int' object is not iterable in Python Ferdy - Sep 30
Java_home is not set and no 'java' command could be found in your path. Bushra Rubab - Sep 26
MySQL will not start without the configured ports free. Bushra Rubab - Sep 26
Object of type datetime is not 'json' serializable. Bushra Rubab - Sep 25
Typeerror: module object is not callable Ankur Ranpariya - Sep 24
TypeError:" float object is not callable" sumitthakur11 - Sep 24
Write a Program to Print First x Terms of the Series 3n+2 which are not Multiples of 4 beekip - Sep 20
Password Authentication Is Not Available For Git Operations zhteja - Sep 20
Java.Lang. illegal State Exception: Fragment Not Attached To Activity zhteja - Sep 19
Failed to load resource: the server responded with a status of 404 (not found) Ankur Ranpariya - Sep 18