[英]trying to connect to postgres (in a docker container) from Ruby on Rails
I am playing around with Docker and wanted to setup postgreSQL in a docker container and then connect to it from a (non dockerized) Ruby on Rails app. 這是 docker 撰寫文件 - 我在這里遵循這種方法。
version: "3"
services:
db:
image: "postgres:13"
container_name: "postgres_dev"
environment:
POSTGRES_PASSWORD: "pgdev2021"
ports:
- "54320:5432"
volumes:
- pgdata://Users/thomas/Documents/Production/PostgreSQL/dbstorage
volumes:
pgdata:
但是當我嘗試運行“Rails db:prepare”時,我得到了這個錯誤:
rails aborted!
ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.54320"?
/Users/thomas/Documents/Websites/rails-frontend/bin/rails:5:in `<top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:10:in `block in <top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:7:in `<top (required)>'
Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.54320"?
/Users/thomas/Documents/Websites/rails-frontend/bin/rails:5:in `<top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:10:in `block in <top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:prepare
(See full trace by running task with --trace)
當我檢查時,容器正在運行
編輯:database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
port: 54320
user: postgres
development:
<<: *default
database: db_development
test:
<<: *default
database: db_test
production:
<<: *default
database: db_production
username: postgres
password: <%= ENV['MY_DB_PASSWORD'] %>
如果我想啟動 Postgres,我總是提供用戶、密碼和初始數據庫。
您是在嘗試進行容器間網絡還是從主機系統訪問? 如果是內部容器網絡,請記住默認情況下服務的主機名等於服務名稱。
撰寫文件示例:
version: "3.9"
services:
postgres:
image: "postgres:13"
container_name: "postgres_dev"
ports:
- 5432:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: my-database
您現在可以從docker-compose.yml
中指定的任何其他service
連接postgres:5432
。 或者從您的主機連接http://localhost:5432
。 更多關於docker 組成網絡。
通過訪問容器進行驗證並運行一些東西:
docker exec -it postgres_dev bash
psql -h localhost -p 5432 -U root -P password -d my-database
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.