I host a Postgres 12 instance using docker on my local machine.
It can be reached using username postgres
and password postgres
and the database is test
. I tested that the instance is running and connections are possible using the provided credentials with a differenct client (e.g. TablePlus).
But a brand new Ruby on Rails project throws the following error at me:
connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket?
my database.yaml
file looks like this:
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: postgres
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
username: postgres
password: postgres
database: test
test:
<<: *default
username: postgres
password: postgres
database: test
production:
<<: *default
database: <%= ENV["DB_DATABASE"] %>
username: <%= ENV["DB_USERNAME"] %>
password: <%= ENV["DB_PASSWORD"] %>
Any ideas on what's going on?
(I'm on an M1 Mac, btw)
CodePudding user response:
You need to provide the host and port number to connect to the database running on docker.
Add these two lines ...
default: &default
...
host: <%= ENV.fetch('DATABASE_HOST', 'localhost') %>
port: <%= ENV.fetch('DATABASE_PORT', 5432) %>
If you look at a fresh database.yml file generated by rails, you will find these lines...
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
Domain socket works when processes run on the same host OS. Since the database process is on docker which is technically another host, server needs database's TCP Socket address to communicate.