Rails and PostgreSQL: Role postgres does not exist

Ruby on-RailsPostgresqlRake

Ruby on-Rails Problem Overview


I have installed PostgreSQL on my Mac OS Lion, and am working on a rails app. I use RVM to keep everything separate from my other Rails apps.

For some reason when I try to migrate the db for the first time rake cannot find the postgres user. I get the error

 FATAL:  role "postgres" does not exist

I have pgAdmin so I can clearly see there is a postgres user in the DB - the admin account in fact - so I'm not sure what else to do.

I read somewhere about people having issues with PostgreSQL because of which path it was installed in, but then I don't think I would have gotten that far if it couldn't find the db.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Actually, for some unknown reason, I found the issue was actually because the postgresql role hadn't been created.

Try running:

createuser -s -r postgres

Note that roles are the way that PostgreSQL maintains database permissions. If there is no role for the postgres user, then it can't access anything. The createuser command is a thin wrapper around the commands CREATE USER, CREATE ROLE, etc.

Solution 2 - Ruby on-Rails

Recently i got this problem immediately after installing postgres. If it comes immediately after installation, you might be missing the default user, postgres. In that case, you can create default user postgres using below command.

createuser -s -U $USER

Ex: createuser -s -U $USER
enter your required role name: postgres
enter password for your the user: 

It will prompt you to enter required database role name and password Once you complete the process, you can login to the postgres console using below command

psql -U 'your_database_name'

Ex: psql -U postgres
Here, You need to enter the password if you have given any, while creating the user.

Hope it helps :)

Solution 3 - Ruby on-Rails

I was on OSX 10.8, and everything I tried would give me the FATAL: role "USER" does not exist. Like many people said here, run createuser -s USER, but that gave me the same error. This finally worked for me:

$ sudo su
# su postgres
# createuser -s --username=postgres MYUSERNAME

The createuser -s --username=postgres creates a superuser (-s flag) by connecting as postgres (--username=postgres flag).

I see that your question has been answered, but I want to add this answer in for people using OSX trying to install PostgreSQL 9.2.4.

Solution 4 - Ruby on-Rails

This message pops up, when the database user does not exist. Compare the manual here.
Multiple local databases cannot be the explanation. Roles are valid cluster-wide. The manual again:

> Note that roles are defined at the database cluster level, and so are > valid in all databases in the cluster.

You must be ending up in another database-cluster. That would be another server running on the same machine, listening to a different port. Or, more likely, on a different machine.

Could it be that the message comes, in fact, from the remote server?

Solution 5 - Ruby on-Rails

I met this issue right on when I first install the Heroku's POSTGRES.app thing. After one morning trial and error i think this one line of code solved problem. As describe earlier, this is because postgresql does not have default role the first time it is set up. And we need to set that.

sovanlandy=# CREATE ROLE postgres LOGIN;

You must log in to your respective psql console to use this psql command.

Also noted that, if you already created the role 'postgre' but still get permission errors, you need to alter with command:

sovanlandy=# ALTER ROLE postgres LOGIN;

Hope it helps!

Solution 6 - Ruby on-Rails

In the Heroku documentation; Getting started whit rails 4, they say:

> You will also need to remove the username field in your database.yml > if there is one so: In file config/database.yml remove: username: > myapp

Then you just delete that line in "development:", if you don't pg tells to the database that works under role "myapp"

> This line tells rails that the database myapp_development should be > run under a role of myapp. Since you likely don’t have this role in > your database we will remove it. With the line remove Rails will try > to access the database as user who is currently logged into the > computer.

Also remember to create the database for development:

$createdb myapp_development

Repleace "myapp" for your app name

Solution 7 - Ruby on-Rails

Could you have multiple local databases? Check your database.yml and make sure you are hitting the pg db that you want. Use rails console to confirm.

Solution 8 - Ruby on-Rails

The installation procedure creates a user account called postgres that is associated with the default Postgres role. In order to use Postgres, you can log into that account. But if not explicitly specified the rails app looks for a different role, more particularly the role having your unix username which might not be created in the postgres roles.

To overcome that, you can create a new role, first by switching over to the default role postgres which was created during installation

sudo -i -u postgres

After you are logged in to the postgres account, you can create a new user by the command:

createuser --interactive

This will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user.

Pass over a role name and some permissions and the role is created, you can then migrate your db

Solution 9 - Ruby on-Rails

My answer was much more simple. Just went to the db folder and deleted the id column, which I had tried to forcefully create, but which is actually created automagically. I also deleted the USERNAME in the database.yml file (under the config folder).

Solution 10 - Ruby on-Rails

In Ubuntu local user command prompt, but not root user, type

sudo -u postgres createuser username

username above should match the name indicated in the message "FATAL: role 'username' does not exist."

Enter password for username.

Then re-enter the command that generated role does not exist message.

Solution 11 - Ruby on-Rails

I ended up here after attempting to follow Ryan Bate's tutorial on deploying to AWS EC2 with rubber. Here is what happened for me: We created a new app using "

rails new blog -d postgresql

Obviosuly this creates a new app with pg as the database, but the database was not made yet. With sqlite, you just run rake db:migrate, however with pg you need to create the pg database first. Ryan did not do this step. The command is rake db:create:all, then we can run rake db:migrate

The second part is changing the database.yml file. The default for the username when the file is generated is 'appname'. However, chances are your role for postgresql admin is something different (at least it was for me). I changed it to my name (see above advice about creating a role name) and I was good to go.

Hope this helps.

Solution 12 - Ruby on-Rails

After a bunch of installing and uninstalling of Postgres, here's what now seems to work consistently for me with Os X Mavericks, Rails 4 and Ruby 2.

  1. In the database.yml file, I change the default usernames to my computer's username which for me is just "admin".

  2. In the command line I run rake db:create:all

  3. Then I run rake db:migrate

  4. When I run the rails server and check the local host it says "Welcome aboard".

Solution 13 - Ruby on-Rails

You might be able to workaround this by running initdb -U postgres -D /path/to/data or running it as user postgres, since it defaults to the current user. GL!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdamView Question on Stackoverflow
Solution 1 - Ruby on-RailsChris SherlockView Answer on Stackoverflow
Solution 2 - Ruby on-RailsPremView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJackView Answer on Stackoverflow
Solution 4 - Ruby on-RailsErwin BrandstetterView Answer on Stackoverflow
Solution 5 - Ruby on-RailssovanlandyView Answer on Stackoverflow
Solution 6 - Ruby on-RailsYunrockView Answer on Stackoverflow
Solution 7 - Ruby on-RailsJonathan JulianView Answer on Stackoverflow
Solution 8 - Ruby on-RailsMayurView Answer on Stackoverflow
Solution 9 - Ruby on-RailsmaudulusView Answer on Stackoverflow
Solution 10 - Ruby on-RailsRobert CambilView Answer on Stackoverflow
Solution 11 - Ruby on-RailsAndrew ShenstoneView Answer on Stackoverflow
Solution 12 - Ruby on-RailsMichael van HolstView Answer on Stackoverflow
Solution 13 - Ruby on-RailsrogerdpackView Answer on Stackoverflow