Rails and PostgreSQL: Role postgres does not exist

2019-01-16 01:27发布

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.

12条回答
Explosion°爆炸
2楼-- · 2019-01-16 02:15

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).

查看更多
The star\"
3楼-- · 2019-01-16 02:16

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-16 02:17

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.

查看更多
Fickle 薄情
5楼-- · 2019-01-16 02:21

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

查看更多
乱世女痞
6楼-- · 2019-01-16 02:22

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.

查看更多
Ridiculous、
7楼-- · 2019-01-16 02:22

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 :)

查看更多
登录 后发表回答