In Rails, Couldn't create database for {"adapter"=>"postgresql",

39,547

Solution 1

Solved!

Solution was simple to update current installed gems.

Another solution to try is set host: localhost or whatever host you use on development settings on database.yml file.

Solution 2

I installed PG on Mac OS X via Homebrew (I didn't realize it came default in Mac OS X), and had the same problem. I may have made the problem worse by installing the pg gem before installing pg via Homebrew.

I solved this same problem by doing the following:

  1. Make sure that all binaries are pointing to binaries in Cellar directory (/usr/local/Cellar on Mac OS X)
  2. Uninstall pg gem gem uninstall pg
  3. Reinstall with Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config (if you created a rails project with pg setup, this is explained in your database.yml config file)

And it worked!

Solution 3

First, you will need to create your user manually in Postgres, while granting privilege of database creation (AKA createdb) for this user.

To do so, first, open Postgres shell prompt

$ sudo -u postgres psql

Create your user

postgres=# create role Your_DB_username with createdb login password 'YOUR_PASSWORD';

Exit from Postgres shell

postgres=# \q

Do whatever db-related rake task:

$ rake db:create

And it will work!

Solution 4

The answer from zquintana above worked for me with a few small caveats.

My solution (using Homebrew):

  1. If you have the gem installed gem uninstall pg
  2. Remove old copy of postgresql brew uninstall postgresql
  3. Re-install postgresql brew install postgresql
  4. The install notes mention:

To have launchd start postgresql at login:

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

Then to load postgresql now:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Or, if you don't want/need launchctl, you can just run:

postgres -D /usr/local/var/postgres

  1. So run ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

  2. Then launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

  3. Now re-install the gem with the Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config

  4. Now run your rake db:create

Solution 5

In my case this was all I needed: https://dba.stackexchange.com/a/75221

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Share:
39,547
Martin
Author by

Martin

Cloud-based ideas starter. I believe that the evolving future of web-apps goes towards the mixing of great engineering (mostly Agile Craftsmanship) and a refined taste for Visual Arts. Its in the right mashup of those two that elegancy, competitiveness and higher-end product is delivered. Skills: Branding and Identity, Managing, Visual Design, Cloud-based architectures, Agile craftsmanship, Investor https://www.yorokobi.com http://martincaetano.com http://twitter.com/PelCasandra

Updated on May 16, 2020

Comments

  • Martin
    Martin about 4 years

    I'm trying to run rake db:create or rake db:setup but I get the following error. It's strange because rake db:create it's working fine from others folders with other Rails projects.

    user:project user$ rake db:create
    could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `initialize'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `new'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `connect'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:260:in `initialize'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:27:in `new'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:27:in `postgresql_connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:304:in     `new_connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:323:in `checkout_new_connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block (2     levels) in checkout'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `loop'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `block in checkout'
    /Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:260:in `checkout'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:162:in `connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in     `retrieve_connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:110:in `rescue in create_database'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:54:in `create_database'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
    /Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
    /Users/user/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
    Couldn't create database for {"adapter"=>"postgresql", "database"=>"project_test",     "username"=>nil, "password"=>nil, "min_messages"=>"warning"}
    

    This is my database.yml

    development:
      adapter: postgresql
      database: project_development
      username:
      password:
      min_messages: warning
    
    test:
      adapter: postgresql
      database: project_test
      username:
      password:
      min_messages: warning
    
    production:
      adapter: postgresql
      database: project_production
      username:
      password:
      min_messages: warning
    

    Posgree is insatelled on /usr/local/var/postgres. Shouldn't rake try to look it in the same folder instead of /var/pgsql_socket/ ? (I don't have a /usr/local/var/pgsql_socket anyways)

  • JGallardo
    JGallardo over 10 years
    which ones? what was the actual process? I ran into the same errors with same problem but your vague comment is not helping at all.
  • Martin
    Martin over 10 years
    Try adding host: localhost or whatever host you use on your database.yml file
  • JGallardo
    JGallardo over 9 years
    Can you add the full code to show where you added the local host. it would really help people that are landing on this question and curious.
  • calyxofheld
    calyxofheld over 8 years
    Isn't development the only environment you'd connect to localhost in?
  • Dex
    Dex over 7 years
    host: localhost is what I was missing