Today, we are together here for a very common error. Especially around beginners. Until you get used to PostgreSQL’s role structure and initial user configuration you will face this issue for a few times.
TL;DR: su – postgres and psql
Initial User Configuration
Initially, only postgres user is granted to login to server. No not even root user. Look at that example:
root@bbe89c90b5eb:/# psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist
If you want to let root to be able to login PostgreSQL server, you need to define a new role for root user.
Let’s see together:
root@bbe89c90b5eb:/# su - postgres
postgres@bbe89c90b5eb:~$ psql
psql (15.3 (Debian 15.3-1.pgdg110+1))
Type "help" for help.
postgres=# CREATE ROLE root WITH SUPERUSER LOGIN;
CREATE ROLE
postgres=# CREATE DATABASE root;
CREATE DATABASE
postgres=# \q
postgres@bbe89c90b5eb:~$ exit
logout
Let’s login with root again:
root@bbe89c90b5eb:/# psql
psql (15.3 (Debian 15.3-1.pgdg110+1))
Type "help" for help.
root=#
What did we do, we logged in with user postgres, created a new role for root.
Tip of the Day
Here is an another standard, when a user tries to connect to PostgreSQL server, by default, PostgreSQL will search for a database with same name with user.
Today we talked about FATAL: role “username” does not exist error message and it’s solution.
If you have a different case, or any questions, please leave a comment below. And do not forget to subscribe to hear more about recent news about PostgreSQL and Open Source.



Leave a Reply