[英]how to connect Rails app to docker postgres on host machine
I want to run a postgres container like so, without docker compose, and connect to it in a Ruby on Rails app:我想像这样运行一个 postgres 容器,没有 docker compose,并在一个 Ruby on Rails 应用程序中连接到它:
docker run -p 5432:5432 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=myapp_development postgres:13.4
my database.yml file looks like this我的 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
However, when running the app I get the following error:但是,在运行应用程序时,我收到以下错误:
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"?无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接?
I don't want to use docker compose currently我目前不想使用 docker compose
What am I missing?我错过了什么?
After run运行后
docker run -p 5432:5432 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=myapp_development postgres:13.4
I'm able to connect postgresql from my terminal by running我可以通过运行从我的终端连接 postgresql
psql -h localhost -U user myapp_development
So, I guess you just need to add host
option to database.yml
所以,我想你只需要向database.yml
添加host
选项
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.