Role does not exist and unable to create database when using PostgreSQL
Add a username to your database.yml
, might as well use your application's name (or some variant of the name) as the username, I'll use app_name
as a placeholder:
development:
adapter: postgresql
encoding: utf8
database: app_development
pool: 5
username: app_name
password:
Then create the user (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, the next two are inside psql
. Then do your rake db:create
.
The User
user is possibly a default but user
is already taken for other purposes in PostgreSQL so you'd have to quote it to preserve the case if you wanted to use User
as a username:
postgres=# create role "User" login createdb;
You're better off creating one user per-application anyway.
You'll want to do similar things for your test
entry in database.yml
as well.
PostgreSQL error: Fatal: role username does not exist
Use the operating system user postgres
to create your database - as long as you haven't set up a database role with the necessary privileges that corresponds to your operating system user of the same name (h9uest
in your case):
sudo -u postgres -i
As recommended here or here.
Then try again. Type exit
when done with operating as system user postgres
.
Or execute the single command createuser
as postgres
with sudo
, like demonstrated by drees in another answer.
The point is to use the operating system user matching the database role of the same name to be granted access via ident
authentication. postgres
is the default operating system user to have initialized the database cluster. The manual:
In order to bootstrap the database system, a freshly initialized
system always contains one predefined role. This role is always a
“superuser”, and by default (unless altered when runninginitdb
) it
will have the same name as the operating system user that initialized
the database cluster. Customarily, this role will be namedpostgres
.
In order to create more roles you first have to connect as this
initial role.
I have heard of odd setups with non-standard user names or where the operating system user does not exist. You'd need to adapt your strategy there.
Read about database roles and client authentication in the manual.
Postgres: Unable to connect to server. role 'postgres' does not exist
I'm not sure if only one of the things that I did helped or both are required, but here is what I have done:
- changed port to 5433 (this seems to be something for sure needed, but I have tried this before with no luck)
- I created a new role and a new OS user with the same name as the role.
With this two it worked for me.
psql: FATAL: database user does not exist
It appears that your package manager failed to create the database named $user for you. The reason that
psql -d template1
works for you is that template1 is a database created by postgres itself, and is present on all installations.
You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:
createdb
and then see if you can log in again with
psql -h localhost
This will simply create a database for your login user, which I think is what you are looking for. If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package.
Role does not exists in Postgresql
You have first to login in a linux shell as the user postgres
and than create new postgres user with the command createuser
. The system user postgres
is created automatically by the Postgres installer.
Execute this code in the console (change your_username
with a username of your choice):
sudo -u postgres -i
createuser -l -d -P your_username
Better you create a database with the same name too (this makes the login later easier):
createdb your_username -O your_username
Then you should be able to connect in psql with
psql -h localhost -U your_username
psql fatal role does not exist
As pointed out in the comments, your pg_hba.conf seems fine.
Usually, the database will run as the postgres user (check ps aux | grep postgres
to find out the username postgres is running under).
Log in as that user, for example sudo su - postgres
, then create a user matching your normal Ubuntu user account (createuser username
), and finally create a database with that same name and set the owner (-O
) to that database user, like this: createdb -O username username
).
That should make calling psql
work, and pgadmin - as long as you start it as your default user, username - should work as well.
Edit: By default, psql
will use your Linux username as default value for both the database-username and the database-name. You can override the username by using -U someotherusername
, and connect to a different database by adding that DB name to the command line, such as psql someotherdbname
. You might also find psql -l
useful for listing the existing databases.
psql: FATAL: role postgres does not exist
NOTE: If you installed postgres using homebrew, see the comment from @user3402754 below.
Note that the error message does NOT talk about a missing database, it talks about a missing role. Later in the login process it might also stumble over the missing database.
But the first step is to check the missing role: What is the output within psql
of the command \du
? On my Ubuntu system the relevant line looks like this:
List of roles
Role name | Attributes | Member of
-----------+-----------------------------------+-----------
postgres | Superuser, Create role, Create DB | {}
If there is not at least one role with superuser
, then you have a problem :-)
If there is one, you can use that to login. And looking at the output of your \l
command: The permissions for user
on the template0
and template1
databases are the same as on my Ubuntu system for the superuser postgres
. So I think your setup simple uses user
as the superuser. So you could try this command to login:
sudo -u user psql user
If user
is really the DB superuser you can create another DB superuser and a private, empty database for him:
CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
But since your postgres.app setup does not seem to do this, you also should not. Simple adapt the tutorial.
Related Topics
Update Just One Gem with Bundler
How to Map and Remove Nil Values in Ruby
Ruby on Rails Scalability/Performance
Ruby/Rails: Converting a Date to a Unix Timestamp
File.Expand_Path("../../Gemfile", _File_) How Does This Work? Where Is the File
Docker for MAC - Mkmf.Rb Can't Find Header Files for Ruby
Understanding the Gemfile.Lock File
How to Sort a Ruby Hash by Number Value
How to Get Sinatra to Auto-Reload the File After Each Change
Difference Between Has_One and Belongs_To in Rails
Any Success with Sinatra Working Together with Eventmachine Websockets
How to Pass Parameter on 'Vagrant Up' and Have It in the Scope of Vagrantfile
Ruby: How to Iterate Over a Range, But in Set Increments
How to Return Early from a Rake Task
"Which in Ruby": Checking If Program Exists in $Path from Ruby