Reuben
Reuben

Reputation: 2751

Rails: rspec fails after switching to postgreSQL

I'm learning rails and following the Ruby on Rails Tutorial Book.

It had an extra challenge and taught me how to use postgreSQL instead of the default SQlite3.

However, since I have changed to postgreSQL my rspec tests fail that did work with SQlite..

I'm running: bundle exec rspec spec/requests/static_pages_spec.rb and it returns:

Failure/Error: Unable to find matching line from backtrace
     PGError:
       could not connect to server: Permission denied
        Is the server running locally and accepting
        connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

This is what I have in my database.yml for testing:

test:
  adapter: postgresql #sqlite3
  database: sample_app #db/test.sqlite3
  pool: 5

Any thoughts? Thanks!!!

Upvotes: 1

Views: 1438

Answers (3)

Gregism
Gregism

Reputation: 392

I had to downgrade to postgres 9.2.2, which I discovered here: https://github.com/PostgresApp/PostgresApp/issues/109

Worked like a charm!

Upvotes: 0

Craig Ringer
Craig Ringer

Reputation: 325051

This is an interesting problem. The error you're getting:

    PGError:
       could not connect to server: Permission denied
        Is the server running locally and accepting
        connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

... indicates that libpq, as used by the Pg gem in Ruby, cannot open the unix socket in /var/pgsql_socket/.s.PGSQL.5432 because of a file system permissions issue. Most likely your user does not have sufficient permissions on /var/pgsql_socket/. If Mac OS X allows sockets themselves to have permissions it could be permissions on /var/pgsql_socket/.s.PGSQL.5432 its self; I use Linux where socket files can't have permissions and don't know if OS X is different.

Try:

ls -ld /var/pgsql_socket/.s.PGSQL.5432
ls -ld /var/pgsql_socket
ls -ld /var

You can usually work around Mac issues with unix sockets and Pg by using a TCP/IP connection. Just specify host as localhost in your database yml; this will cause a TCP/IP network connection to be made rather than a unix socket connection.

It's also possible you're using a libpq that doesn't match the PostgreSQL server you're running. This is common on OS X because Apple installed a customised older version of PostgreSQL by default on some system versions. If that's the case then there's tons of information about the issue on Stack Overflow (search for the tags 'osx', 'rails', 'postgresql') so I won't repeat it here. Using TCP/IP is a workaround, as is specifying host as the unix_socket_path of the running PostgreSQL server.

It's possible that'll fail with a connection refused if PostgreSQL is not in fact running.

Upvotes: 0

Brad Werth
Brad Werth

Reputation: 17647

Are you really not setting your username and password,or have you simply omitted it for the sake of security? It would appear your credentials are missing or incorrect. Ensure you are specifying the correct username and password. You may also want to specify a port.

For example, try the following:

adapter: postgresql 
database: sample_app
hostname: 127.0.0.1
port: 5432
username: USERNAME
password: PASSWORD

You will need to replace USERNAME and PASSWORD with teh values you used when you set up your Postgres server.

Upvotes: 1

Related Questions