如何将 Rails 应用程序连接到主机上的 docker postgres

how to connect Rails app to docker postgres on host machine

我想 运行 像这样的 postgres 容器,没有 docker 组合,并在 Rails 应用上的 Ruby 中连接到它:

docker run -p 5432:5432 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=myapp_development postgres:13.4

我的 database.yml 文件看起来像这样

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: myapp_development
  user: user
  password: password

但是,当 运行 运行该应用程序时,出现以下错误:

could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

我不想使用 docker 目前撰写

我错过了什么?

运行

之后
docker run -p 5432:5432 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=myapp_development postgres:13.4

我可以通过 运行ning

从我的终端连接 postgresql
psql -h localhost  -U user myapp_development

所以,我猜你只需要将 host 选项添加到 database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
  host: localhost

development:
  <<: *default
  database: myapp_development
  user: user
  password: password