如何在rails console中显示数据库的连接信息

快速查询当前数据库连接信息

Rails.configuration.database_configuration[Rails.env]

OR

Rails.application.config.database_configuration[Rails.env]

OR

ActiveRecord::Base.connection_config

显示Development环境下的数据库连接信息

[1] pry(main)> config   = Rails.configuration.database_configuration
=> {"development"=>{"adapter"=>"postgresql", "encoding"=>"utf8", "reconnect"=>false, "database"=>"database", "pool"=>5, "username"=>"xxx", "password"=>nil},
 "test"=>{"adapter"=>"postgresql", "encoding"=>"utf8", "reconnect"=>false, "database"=>"database_test", "pool"=>5, "username"=>"xxx", "password"=>nil},
 "production"=>{"adapter"=>"postgresql", "encoding"=>"utf8", "reconnect"=>false, "database"=>"database", "pool"=>5, "username"=>"xxx", "password"=>nil}}
[2] pry(main)> host = config[Rails.env]["host"]
=> nil
[3] pry(main)> database = config[Rails.env]["database"]
=> "database"
[4] pry(main)> username = config[Rails.env]["username"]
=> "xxx"
[5] pry(main)> password = config[Rails.env]["password"]
=> nil

Ref: https://guides.rubyonrails.org/configuring.html#connection-preference