Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: rake db:create:all fails to connect to PostgreSQL database

I am trying to create a Rails app that uses PostgreSQL. Here is a description of what I did.


PostgreSQL setup:
I installed PostgreSQL 9.1.3 via the ppa:pitti/postgresql maintained by Martin Pitt. There was PostgreSQL 8.4 installed before; I am not sure if it is still installed or gone.

  • I added a database user with superuser rights to the database that has the same name as my Ubuntu account.
  • I start the database daemon with sudo service postgresql start.
  • I installed pgadmin3, Version 1.14.0 Beta 1 via ppa:rhonda/pgadmin3 maintained by Gerfried Fuchs.
  • I can connect via pgadmin3 using my user account and password and port 5433.

My postgres configuration in pg_hba.conf is as follows (removed comments for readability).

[...]
local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

Rails setup:
Now I want to create a Rails application that uses PostgreSQL.

  • I installed Ruby 1.9.3-p125 via RVM.
  • I installed Rails 3.2.3 into the Gemset ruby-1.9.3-p125@global.
  • I created a .rvmrc and Gemset for the application.
  • I created a Rails application via rails new my_test_app -d postgresql.
  • I configured the user name and password in config/database.yml for development and test and removed production.
  • I configured host: localhost and port: 5433 in config/database.yml.

Here is the content of my config/database.yml (removed comments for readability).

development:
  adapter: postgresql
  encoding: unicode
  database: my_test_app_development
  pool: 5
  username: johndoe
  password: password    
  host: localhost
  port: 5433

test:
  adapter: postgresql
  encoding: unicode
  database: my_test_app_test
  pool: 5
  username: johndoe
  password: password

Problem:
However, when I run bundle exec rake db:create:all I receive the following error message.

could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket
"/var/run/postgresql/.s.PGSQL.5432"?
[...]
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode",
"database"=>"my_test_app_test", "pool"=>5, "username"=>"johndoe",
"password"=>"password"}

Question:
Why is the port different to the one I use when I successfully connect via pgadmin3?

like image 765
JJD Avatar asked Apr 21 '12 23:04

JJD


3 Answers

@Riateche: Finally, I saw that the database configuration for test environment misses the explicit settings for host and port. After I added the settings to the test environment, I was able to run the command bundle exec rake db:create:all successfully.
I must say, I do not like that they suggest those settings for the development enviroment, but did not add them for the other environments. That makes it very likely to miss them, as I proofed.

test:
  adapter: postgresql
  encoding: unicode
  database: my_test_app_test
  pool: 5
  username: johndoe
  password: password
  host: localhost
  port: 5433
like image 192
JJD Avatar answered Nov 07 '22 06:11

JJD


If any psql client session is accessing template1 ( for example psql or pgAdmin ), rake db:migrate fails. Close any sessions before doing rake db:migrate .

like image 1
Anish Avatar answered Nov 07 '22 06:11

Anish


you can change your postgresql configuration in pg_hba.conf to trust.

[...]
local   all             postgres                                peer
local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
like image 1
Hetdev Avatar answered Nov 07 '22 08:11

Hetdev