- Read Tutorial
- Watch Guide Video
Let's first try to create this error here. I'm going to generate a new rails project, so I'm going to say rails new MyPgProject -T --database=postgressql
. I'm going to have it skip the test, so it loads a little bit faster. Then I'm also going to make sure that I am using the database of PostgreSQL
.
Now, this assumes that you already have Postgres installed on your system, so I'm going to run this. It's going to go install all the dependencies, and bundle install and everything like that. Now, if I type ls
, you can see that we have MyPgProject
right there.
Let's switch into that, cd MyPgProject/
and you may think that we'd be able to type rails db:create
just like this, and usually that will work. If you run into the rule does not exist bug
, like we're going to right here, then this is going to break.
If I run this then you're going to see that we get this giant very scary looking error message. Scroll all the way up to the top. Then you're going to see where it says PG::ConnectionBad Fatal role "jordan" does not exist
.
Now, do not let this intimidate you. All it means is that that Postgres was looking for the username JORDAN
. It says role, but it's actually looking for a username Jordan
, and it was not able to find it. That is what the entire issue is related to.
I'm going to show you that we can fix it just like this. The very first thing that we have to do is we have to switch in and use our Postgres user. To do that type sudo su
. That stands for super user
. Then sudo su - postgres
. What this means is right now we're logged in as Jordan, well, I am.
Yours is going to say whatever your name is, but you can see right here I'm logged in as Jordan into this version of Linux. I want to be logged in as a Postgres user
. If I hit enter. It's going to ask me for my system password. Now you can see that it says postgres@jordan
. Now I am logged in as the Postgres user.
Now what I can do is create that user, so I can say create user -s -r jordan
. Most likely, unless your name is Jordan
, and you gave Jordan
as your system role or your system name, then this is going to be your name is.
Whenever you have that error and it says "role does not exist" for mine it says Jordan
. Yours is going to say whatever your name is. That is what you want to type in. So createuser - s -r jordan
. Hit enter, and that's it.
Now hit control + d
. That's going to switch you back into your default user. Assuming that you're still in the project. Now let's try rails db:create
once again, and you're going to see that this is actually working. We no longer get that error message.
You can do a really quick scaffold, so you can say rails g scaffold Blog title:string
, just like this. Then it's going to generate all this for us. Then we're going to be able to update the database. Now I can say rails db:migrate
.
This is going to update the database tables that we already created. Type rails s
to start the rails server. Now we should be able to go to localhost 3000 in our browser, and update everything.
We can actually type different titles in for blogs and different things like that just to make sure that we actually have persistence. That what we're typing into the screen is going to be able to be saved in the database.
If I type localhost3000/blogs
, you can see that now we have our blogs showing up. I can type new title My test
, hit create blog, and that is stored.
That means that this is stored in the database, which means that our database configuration is working perfectly. That is how you can fix the role not found
, and fix that fatal Postgres
error when you're working on Linux.