29

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

2
  • 4
    Try rails db from your application root see to which database it connects. It should open postgres console with inspector_development=# if this is your database for development Commented Apr 26, 2015 at 6:57
  • I usually restore it to a different database name and point database.yml to that one. Haven't had issues with this approach so far. Commented Apr 26, 2015 at 7:08

2 Answers 2

54

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

This didn't work for me in rails 6, (possibly just some funkyness with our setup) but ActiveRecord::Base.connection.current_database did at least provide the database name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.