Can You Get Db Username, Pw, Database Name in Rails

Can you get DB username, pw, database name in Rails?

From within rails you can create a configuration object and obtain the necessary information from it:

config   = Rails.configuration.database_configuration
host = config[Rails.env]["host"]
database = config[Rails.env]["database"]
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]

See the documentation for Rails::Configuration for details.

This just uses YAML::load to load the configuration from the database configuration file (database.yml) which you can use yourself to get the information from outside the rails environment:

require 'YAML'
info = YAML::load(IO.read("database.yml"))
print info["production"]["host"]
print info["production"]["database"]
...

how to check the database name that ActiveRecord uses


Rails.configuration.database_configuration

This will give you a hash table with the configurations for each of your environments. E.g. to get your development database name:

Rails.configuration.database_configuration["development"]["database"]

Rails - ActiveRecord get database name from an Instance

If you are using mysql you probably can use:

Timetable.connection.execute("select database()").first.first

The other databases might have a query to get the current db too.

How to find database name in rails console

This works in Rails 3 and Rails 4

ActiveRecord::Base.connection.current_database

But this only works with drivers that have implemented that method. For example, it will not work for SQLite but will work with mysql and postgres.

Displaying host and database names in Rails

You can create a rails configuration object and obtain the necessary information from it:

config   = Rails::Configuration.new
host = config.database_configuration[RAILS_ENV]["host"]
database = config.database_configuration[RAILS_ENV]["database"]

See the documentation for Rails::Configuration for details.

Rails connects to database without username or password

Rails uses the Ruby Pg gem, which uses PostgreSQL's libpq client library.

libpq uses the current system user name if no user name is supplied.

No password is required by the server if peer, ident sameuser or trust authentication is used in pg_hba.conf. If the server doesn't request a password, libpq doesn't try to send one and doesn't care if one is missing.

You can actually supply the empty string for the database too; libpq will then use the db that's the same as your username if it exists.

Rails trying to get external database info with ActiveRecord::Base.connection_config

I dug around a bit more and found an answer at

Stack overflow post

The solutions is to put <%= CustomerTable.connection_config %> in the view.

Inserting default admin user into database during Rails App startup

Use seed data. Run rake db:setup to create the databases, load the schema, and load the seed data. Or, if the database has already been set up, you can just run rake db:seed to just load the seed data.

Laravel connec to database with user inputed username and password

You can get the user inputs into a controller function and update the config/datadata.php. Assuming the driver is mysql by default, you can do this:

public function checkDatabaseConnection(Request $request)
{
//update the config
config(['database.connections.mysql' => [
'host' => $request->host,
'username' => $request->username,
'password' => $request->password
]]);

//Check the credentials by calling PDO
try {
DB::connection()->getPdo();
} catch (\Exception $e) {
return redirect()->back()->withErrors(["connection" => "Could not connect to the database. Please check your input."]);
}
}

Don't forget to add use DB at the top of your controller.



Related Topics



Leave a reply



Submit