9

I am trying to create a postgresql database for my rubyonrails application and I followed the following steps post installation:

sudo -u postgres -i
psql
CREATE USER rohitrp
CREATE DATABASE library_development OWNER rohitrp
CREATE DATABASE library_test OWNER rohitrp

My database.yml looks like this:

development:
  adapter: postgresql
  encoding: unicode
  database: library_development
  username: rohitrp
  password:
  pool: 5
  timeout: 5000

test:
  adapter: postgresql
  encoding: unicode
  database: library_test
  username: rohitrp
  password:
  pool: 5
  timeout: 5000

When I run rake db:setup I get the following error:

PG::InsufficientPrivilege: ERROR:  permission denied to create database
: CREATE DATABASE "library_development" ENCODING = 'unicode'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/schema_statements.rb:78:in `create_database'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `create'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:93:in `create'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/home/rohitrp/.rbenv/versions/2.2.1/bin/rake:33:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"library_development", "username"=>"rohitrp", "password"=>nil, "pool"=>5, "timeout"=>5000}
PG::InsufficientPrivilege: ERROR:  permission denied to create database
: CREATE DATABASE "library_test" ENCODING = 'unicode'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/schema_statements.rb:78:in `create_database'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `create'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:93:in `create'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/home/rohitrp/.rbenv/versions/2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/home/rohitrp/.rbenv/versions/2.2.1/bin/rake:33:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"library_test", "username"=>"rohitrp", "password"=>nil, "pool"=>5, "timeout"=>5000}
-- create_table("books", {:force=>:cascade})
rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "library_development" does not exist
/home/rohitrp/RubymineProjects/library/db/schema.rb:16:in `block in <top (required)>'
/home/rohitrp/RubymineProjects/library/db/schema.rb:14:in `<top (required)>'
PG::ConnectionBad: FATAL:  database "library_development" does not exist
/home/rohitrp/RubymineProjects/library/db/schema.rb:16:in `block in <top (required)>'
/home/rohitrp/RubymineProjects/library/db/schema.rb:14:in `<top (required)>'
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)

For resolving the permission issues I followed the answer on this link Postgres permission denied to create database on rake db:create:all

I am still facing the same error.

Community
  • 1
  • 1
user1692342
  • 5,007
  • 11
  • 69
  • 128
  • Your user `rohitrp` doesn't have privileges to create database. But why would you run `db:setup` since you have already created the database? – Marc-Alexandre Bérubé Sep 14 '15 at 19:15
  • @Marc-AlexandreBérubé When I run db:migrate I get ActiveRecord::NoDatabaseError: FATAL: database "library_development" does not exist – user1692342 Sep 14 '15 at 19:18
  • in psql if you type `\list` , what do you get? – Marc-Alexandre Bérubé Sep 14 '15 at 19:27
  • @Marc-AlexandreBérubé its empty – user1692342 Sep 14 '15 at 19:32
  • 6
    Use the command `ALTER USER rohitrp CREATEDB;` to grant the user `rohitrp` the ability to create Databases in PostgreSQL (use this command in the `psql` console, you also need to be logged-in as a superuser, such as the user `postgres`) – MrYoshiji Sep 14 '15 at 20:14
  • @MrYoshiji I tried that. Still permission denied. I am running db:setup as rohitrp, is that fine? – user1692342 Sep 14 '15 at 20:20
  • after using the command I gave you, what was the PostgreSQL output? Did it seem like it ran successfully? You could directly use the user `postgres` instead of `rohitrp` in your database.yml but I guess it is better to use another user account. What you are doing is fine, it is just "weird" that you can't grant the privilege to your `rohitrp` user. – MrYoshiji Sep 14 '15 at 20:23
  • It ran successfully. No errors – user1692342 Sep 14 '15 at 20:24
  • Then try to run the following command in your terminal (linux console): `psql -U rohitrp`. It should start a `psql` console and you should be authenticated as `rohitrp`. From there, try a simple `CREATE DATABASE library_test OWNER rohitrp;` and see if it runs successfully. If not -> your user `rohitrp` probably does not have the privilege to create DBs. – MrYoshiji Sep 14 '15 at 20:27
  • I get an error psql: FATAL: Peer authentication failed for user "rohitrp" – user1692342 Sep 14 '15 at 20:33
  • @MrYoshiji I am still stuck ! :( – user1692342 Sep 17 '15 at 01:59
  • you should create a db/table from a user/role (registered in PG DB) with proper rights to create db (usually admin) – Малъ Скрылевъ Dec 07 '15 at 11:40

2 Answers2

17
$ psql -U postgres
postgres=# ALTER USER username CREATEDB;
eugene_trebin
  • 1,485
  • 1
  • 16
  • 29
13

I was also facing this problem when I was doing setup of my Ruby on Rails app.Then,

  1. sudo -u postgres psql

    This will make you enter into psql shell.

Once you enter your shell type:

  1. ALTER USER username CREATEDB;

In the place of username,you have to add your default system username in most of the cases.

enter image description here

Community
  • 1
  • 1
hardik chugh
  • 1,082
  • 15
  • 12