title: PG::InsufficientPrivilege: ERROR: permission denied to create database Ruby on rails application
body: <p>I am trying to create a postgresql database for my rubyonrails application and I followed the following steps post installation:</p>
  
  <pre><code>sudo -u postgres -i
  psql
  CREATE USER rohitrp
  CREATE DATABASE library_development OWNER rohitrp
  CREATE DATABASE library_test OWNER rohitrp
  </code></pre>
  
  <p>My database.yml looks like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>When I run rake db:setup I get the following error:</p>
  
  <pre><code>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 &lt;top (required)&gt;'
  /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 `&lt;main&gt;'
  Couldn't create database for {"adapter"=&gt;"postgresql", "encoding"=&gt;"unicode", "database"=&gt;"library_development", "username"=&gt;"rohitrp", "password"=&gt;nil, "pool"=&gt;5, "timeout"=&gt;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 &lt;top (required)&gt;'
  /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 `&lt;main&gt;'
  Couldn't create database for {"adapter"=&gt;"postgresql", "encoding"=&gt;"unicode", "database"=&gt;"library_test", "username"=&gt;"rohitrp", "password"=&gt;nil, "pool"=&gt;5, "timeout"=&gt;5000}
  -- create_table("books", {:force=&gt;:cascade})
  rake aborted!
  ActiveRecord::NoDatabaseError: FATAL:  database "library_development" does not exist
  /home/rohitrp/RubymineProjects/library/db/schema.rb:16:in `block in &lt;top (required)&gt;'
  /home/rohitrp/RubymineProjects/library/db/schema.rb:14:in `&lt;top (required)&gt;'
  PG::ConnectionBad: FATAL:  database "library_development" does not exist
  /home/rohitrp/RubymineProjects/library/db/schema.rb:16:in `block in &lt;top (required)&gt;'
  /home/rohitrp/RubymineProjects/library/db/schema.rb:14:in `&lt;top (required)&gt;'
  Tasks: TOP =&gt; db:schema:load
  (See full trace by running task with --trace)
  </code></pre>
  
  <p>For resolving the permission issues I followed the answer on this link <a href="https://stackoverflow.com/questions/28116927/postgres-permission-denied-to-create-database-on-rake-dbcreateall">Postgres permission denied to create database on rake db:create:all</a></p>
  
  <p>I am still facing the same error.</p>