我在本地机器上使用docker托管Postgres 12实例。可以使用用户名postgres和密码postgres访问它,数据库是test。我测试了实例正在运行,并且可以使用不同客户端(例如TablePlus)提供的凭据进行连接。
但是一个全新的Rails项目向我抛出了以下错误: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?
我的database.yaml文件如下所示:
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"] %>对发生了什么有什么想法吗?
(我在M1 Mac上,顺便说一句)
发布于 2022-07-14 21:11:24
您需要提供主机和端口号,以连接到在docker上运行的数据库。
加上这两行..。
default: &default
...
host: <%= ENV.fetch('DATABASE_HOST', 'localhost') %>
port: <%= ENV.fetch('DATABASE_PORT', 5432) %>如果您查看由rails生成的新的database.yml文件,您会发现这些行.
# 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当进程在同一个主机操作系统上运行时,域套接字可以工作。由于数据库进程在技术上是另一个主机的码头上,服务器需要数据库的TCP 套接字地址进行通信。
https://stackoverflow.com/questions/72985511
复制相似问题