PostgresQL: PG::ConnectionBad: FATAL: role "username" does not exist

Hey guys, how have you been?

Today, we are going to learn how to solve the following error message:

PG::ConnectionBad: FATAL:  role "username" does not exist

Introduction

The error message happens when you try to create a Rails database and the role is not found.

In my example, calaca is the role.

1 - Check if it's installed

Just to make sure that Postgresql is already installed.

which psql

You should get something like this


2 - Check the version

psql --version

or

psql --version

You should get something like this


3 - Start the PostgresQL service

sudo service postgresql start
sudo service postgresql status

You should get something like this


4 - Enter the PostgresQL terminal

sudo -u postgres psql

You should be able to see something like this:


5 - Create the role

In my example, calaca is my role.

CREATE USER calaca SUPERUSER PASSWORD 'yourpassword';

6 - Check if the new role is already available

\du

You should be able to see something like this

You


7 - Create the databases

CREATE DATABASE "database_name_development";
CREATE DATABASE "database_name_test";

8 - Check if all databases have been created

\l

You should be able to see something like this


9 - Set the role to own the databases

ALTER DATABASE database_name_development OWNER TO username;
ALTER DATABASE database_name_test OWNER TO username;

In my example, calaca is my role.

The confirmation message is ALTER DATABASE.


10 - Check if the databases belong to your role

\l

You should be able to see something like this


11 - Leave the terminal

\q

You should see something like this


12 - Create Rails databases

rails db:create

You'll get a message that they already exist


13 - Celebrate

image.png

image.png

Conclusion

That's all for today. I hope this article helped you. Let me know if you have any questions.