这里有一些有用的信息:
https://guides.rubyonrails.org/v4.2.7/configuring.html#configuring-a-database
第 3.12 和 3.13 节
我认为您的问题可能是主机:设置。这应该要么
是 /tmp 用于 unix 域套接字,或者是 IP 地址/主机名,如果使用的话。
我在测试中使用的设置:
postgresql 12.3 debian unix。
Using this pg_hba.conf:
local all all ident
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host all all 192.168.50.0/24 md5
您可能需要一个 hostssl 条目,用于在机器之间发送数据。
# using the same "user" database name you have.
rails new user -d postgresql
#setting the password for testing.
psql
alter role user1 password 'testpass';
cd user/config
emacs database.yml
#example using ident authentication - being logged in as the same unix user.
production:
<<: *default
host: /tmp
database: user_production
username: user1
password: nil
# using a host ip address
production:
<<: *default
host: 192.168.50.6
database: user_production
username: user1
password: testpass
# using a host ip address with env vars
#export USER_DATABASE_USERNAME="user1"
#export USER_DATABASE_PASSWORD="testpass"
production:
<<: *default
host: 192.168.50.6
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
# hardcoding everything in the url for illustration:
production:
<<: *default
url: postgres://user1:testpass@192.168.50.6/user_production
# best practice according to doc. DATABASE_URL has precedence over database.yaml when set
# we are just indicating we know that here.
# export DATABASE_URL="postgres://user1:testpass@192.168.50.6/user_production"
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
user1@debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:create
Created database 'user_production'
user1@debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:drop
Dropped database 'user_production'
#Seeing what is being used:
RAILS_ENV=production bin/rails runner 'p ActiveRecord::Base.configurations'
#<ActiveRecord::DatabaseConfigurations:0x0000564e40093fa0 @configurations=[#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e40092f60 @env_name="default", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400928f8 @env_name="development", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_development"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400921a0 @env_name="test", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_test"}>, #<ActiveRecord::DatabaseConfigurations::UrlConfig:0x0000564e40091b60 @env_name="production", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :username=>"user1", :password=>"testpass", :database=>"user_production", :host=>"192.168.50.6"}, @url="postgres://user1:testpass@192.168.50.6/user_production">]>