Jacek Kwiecień Jacek Kwiecień - 4 days ago 6
Ruby Question

ActiveRecord::Base.establish_connection with postgresql on AWS

When I got my database.yml configured like this:

default: &default
adapter: postgresql
encoding: utf8
pool: 5
timeout: 5000

production:
<<: *default
host: my_db_address
port: 5432
database: my_db_name
username: my_db_user_name
password: my_db_password

< test and development ommited >


When I establish connection like this:

ActiveRecord::Base.establish_connection


it says
ActiveRecord::AdapterNotSpecified - 'production'


It works hovewer If I do it like this:

ActiveRecord::Base.establish_connection(
{:adapter => 'postgresql',
:database => 'my_db_name',
:host => 'my_db_address',
:port => '5432',
:username => 'my_db_user_name',
:password => 'my_db_password'}
)


I'd rather load the config from database.yml. How do I do this?

I'm on Rails 4.2.1 and Postgres 9.4

Answer

Here is the working code:

db_config = YAML.load_file('config/database.yml')
ActiveRecord::Base.establish_connection(db_config['production'])
Comments