Can't connect to Postgresql server, locally
I have a Rails application that uses sqlite3 for a DB. Deployed to Hereku. Then find out what Heroku recommends to switch to PostgreSQL. So now I'm trying to switch with no luck. tried to use this Railscasts video for reference .
I have installed Homebrew. Installed PostgreSQL via Homebrew. There was no mention of creating a username or password for postgres after installation.
I edited my Gemfile to
gem 'pg'
for production and development and bundle install
.
I edited the file database.yml
for this:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
As Ryan says in the video, I try this command:
rake db:create:all
Just to get this error:
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"?
I do some more searches and see some tutorials showing the username and password included in the file database.yml
. Then I will learn how to set up a user for Postgresql
After entering the command, $ createuser joe
I was never presented with the options that the docs say. For example, "Should the new role be superuser?" (Y / n) "So really not sure if the user was created, but there were no errors.
So my guess is that after creating the "joe" user, I re-posted the file database.yml
to include the user field I just created:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
Only to get the same error as when unable to connect.
I executed the command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running.
Just in case when needed, when I run
which psql
I get this:
/usr/local/bin/psql
Is there something I am missing? Part of the database name database.yml file
. Is it supposed to be a database already created somewhere, or does this file create the database when the command is run rake db:create:all
? I'm assuming the latter, so the database name doesn't matter?
source to share
Lazy option: add host: localhost
to your database.yml
.
Just a slightly less lazy option: Remove and reinstall the gem pg
.
What's going on here? There are several ways in which Postgres already exists on your system, and pg
gem will use output pg_config
and build for its own needs if you install the gem before installing your own copy of Postgres.
In your case, it was built for the version included in some releases of Mac OS X that uses a socket file in /var/pgsql_socket
.
source to share
when you install Postgres through Homebrew it will add your username to postgres with a blank password.
Just open a command prompt and type
whoami
Then change your database.yml to your Mac username (which is returned from whoami) with a blank password.
Maybe add host: localhost to you database.yml
source to share