sam452
sam452

Reputation: 1321

How to find database name in rails console

This is odd. In my Rails 4 database.yml I have the following:

development:
  adapter: postgresql
  encoding: unicode
  database: inspector_development
  password: 
  pool: 5

I copied the production database from Heroku and imported it using this form into my local copy of Postgresql

curl -o latest.dump `heroku pgbackups:url --account personal`
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U sam -d inspector_development latest.dump

THe result showed the 88 expected users in PGadmin in the inspector_development database on osx. However, even after restarting the rails app, the User table still shows only one user, not the 88 I see in PGadmin.

I've googled for how to determine what Rails sees as the properties of the database name in order to determine where is it finding these non-existent records?

Looking more closely at the User table in PGadmin I see zero columns. Perhaps PGadmin is mistaken? I'm unable to determine where the db Rails is looking for so that I can troubleshoot this, thx, sam

Upvotes: 29

Views: 24703

Answers (2)

W4rQCC1yGh
W4rQCC1yGh

Reputation: 2219

For Postgres what also works is ActiveRecord::Base.connection.config which will give you more detailed output about all your database configuration:

=> {:adapter=>"postgresql", :encoding=>"unicode", :port=>5432, :host=>"appdb", :pool=>5, :username=>"postgres", :password=>"example", :database=>"app_dev"}

This works in Rails 5.2.6*

EDIT: Thanks @masukomi for the comment. Looks like they removed the .config method. As I'm using Rails 7.0.3 now then I checked how does it work there. Turns out the same:

ActiveRecord::Base.connection.current_database

Upvotes: 4

Pramod Shinde
Pramod Shinde

Reputation: 1892

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.

Upvotes: 53

Related Questions