The role does not exist and the database cannot be created using PostgreSQL

I use Heroku for my application and it requires PostgreSQL, but you can still use SQLite3 for development. Since Heroku strongly recommended not having 2 different databases, I decided to upgrade to PostgreSQL for development. I installed gem pg , and also went to the official PostgreSQL website to get the Windows installer, and also modified my database.yml . During installation, it requires a password for PostgreSQL, so I created it. I had to change the pg_hba.conf using md5 to trust in order to get past: fe_sendauth: no password supplied when trying to create a database.

 # TYPE DATABASE USER ADDRESS METHOD # IPv4 local connections: host all all 127.0.0.1/32 trust # was md5 # IPv6 local connections: host all all ::1/128 trust # was md5 # Allow replication connections from localhost, by a user with the # replication privilege. #host replication postgres 127.0.0.1/32 trust #host replication postgres ::1/128 trust 

After getting rid of this, now I get the following:

 $ rake db:create (in C:/app) FATAL: role "User" does not exist Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "database"=>"app_test", "pool"=>5, "username"=>nil, "password"=>nil} 

I still have my development.sqlite3 and text.sqlite3 present, maybe a problem? What should be done?

Here is my full meaning: https://gist.github.com/1522188

+56
ruby ruby-on-rails postgresql ruby-on-rails-3
Dec 26 '11 at 22:05
source share
3 answers

Add the username to your database.yml , you can also use your application name (or some variant of the name) as the username, I will use app_name as a placeholder:

 development: adapter: postgresql encoding: utf8 database: app_development pool: 5 username: app_name password: 

Then create the user (the "AKA" role) inside PostgreSQL using psql.exe :

 $ psql -d postgres postgres=# create role app_name login createdb; postgres=# \q 

The first line is in your terminal, and the next two are inside psql . Then do your rake db:create .

User is probably the default, but User already accepted for other purposes in PostgreSQL, so you have to quote it to save this if you want to use User as your username:

 postgres=# create role "User" login createdb; 

In any case, you better create one user for each application.

You will also want to do similar things for your test entry in database.yml .

+118
Dec 26 '11 at 22:44
source share

PostgreSQL will try to create a database with your account name (login) if username not specified username . On OS X and Linux, you can see who it is with whoami . It looks like you are using Windows.

Solution A: Create a PostgreSQL user that matches the one he is looking for. for example

 createuser --superuser some_user 

Solution B: Change the database user by explicitly specifying the username as shown in the mu answer .

+6
Feb 10 '16 at 12:31 on
source share

If you have a specific account / user on your computer for postgres called postgres, for example.

Then executing this command will prompt you to enter a role name.

 sudo -u postgres createuser --interactive 

Then do

 rake db:create 

Must work!

+4
Jun 07 '17 at 5:37 on
source share



All Articles