Correctly configuring a Postgres DB for a Rails 4.2 app on Heroku - ruby-on-rails

I'm confused about how to configure the DB in a Rails 4.2 app that uses Postgres and Heroku.
Following the advice in this Heroku guide, you'll get a config/database.yml like this:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
production:
<<: *default
database: app_name_production
But when I tried this, my development and test environment were using the same DB as the staging environment (note the file has no configuration for staging). That's no good.
This Heroku guide for connecting to the DB in Ruby mentions that any Rails apps before 4.2 would have their database.yml file overwritten by Heroku. Heroku will parse the DATABASE_URL environment variable and create a new database.yml file.
So I guess it's possible to just leave out the configuration in database.yml for any environments that you have on Heroku, such as staging and production. Your database.yml file could essentially look like Hound's (note the lack of a production configuration).
development: &default
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
test:
<<: *default
database: app_test
But since we're using Rails 4.2, I don't think Heroku will override the database.yml file. In that case, do you have to specify DB configuration in database.yml for our environments on Heroku? Or is it still safe to leave them out? If we do need to specify the configuration for Heroku environments, will the following be sufficient?
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
I'm also confused about the proper configuration for the development and test environments. As I mentioned above, the first configuration shown has those environments using the staging DB on Heroku, instead of a local DB.
This Heroku guide says to export the DATABASE_URL environment variable for your app to connect (once Postgres is installed and you can connect to it).
Assuming you export the DATABASE_URL env var as specified in the article, what does your configuration for development and test have to look like? Do we go with the configuration as shown in the first guide, e.g.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
Or do we use a configuration as shown in this Heroku guide (which uses host and username)
development:
adapter: postgresql
host: localhost
username: user
database: app-dev
Update 1: Here's what I now know. A staging and production config isn't necessary in config/database.yml if you deploy to Heroku, no matter your Rails version. Prior to 4.2, Heroku would generate it's own database.yml file based on the value of the DATABASE_URL environment variable, overwriting your config file (if it exists). Since Rails 4.2, your app will use the DATABASE_URL environment variable directly (bypassing the database.yml file), so Heroku doesn't need to (and won't) generate a config file.
I also figured out why my development and test environments were using the remote staging DB from the Heroku app instead of the local DBs specified in their database.yml config. It was because my local .env file for development is based off of my staging .env file which contains environment variables for connecting to the database such as DATABASE_URL. Because DATABASE_URL was present in my development .env file, my Rails 4.2 app was using it and thus connecting to the staging DB. To fix it, I removed those environment variables from the development .env file and created the local DBs (and ran the migrations) with bundle exec rake db:setup.
Update 2: This section of the Rails Guides goes into more detail about how to configure the DB, worth a read: http://guides.rubyonrails.org/configuring.html#configuring-a-database

Most of your assumptions are correct. The following is a reasonable database.yml configuration file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
Make sure that the RAILS_ENV is properly set on Heroku (either staging or production), or Rails will default to development.
Locally, the test will pick the test environment. By default, the app will start in development mode, using the development environment.

In fact, many developers choose to ignore the database.yml in version control and never publish it into the repo. The reason for that is, that databases may be different on different machines, that's a reason not to keep the configuration common.
I'm working on a Rails 4.2 project right now, and Heroku has no problem with having no database.yml at all (both with PostgreSQL and MySQL, we tested both). Why? Because DATABASE_URL provides all the information necessary to access the database, even adapter name. How? Here's the formula:
adapter://username:password#hostname:port/database?options
Locally, I'm using Postgres with peer authentication: the database server assumes the same username that I'm using in my OS, username is covered, password is useless. Local machine is assumed when no host is given, although I can't tell if it tries to communicate via TCP/IP or Unix domain sockets, so I'm fine without host.
So the configuration you refer to as "shown in the first guide" is reasonable: it contains a minimum amount of settings and allows you to create more environments quite easily.

Heroku doesn't create a database.yml on rails 4.2 because as of that version rails will detect the presence of that environment variable and use it to configure the database connection.
Adding
production:
url: <%= ENV['DATABASE_URL'] %>
Makes it a little more obvious what is happening for those who might not be aware of it but won't change the behaviour. The configuring rails guide has more info in interactions between database.yml and DATABASE_URL if you need it.

To connect with ActiveRecord without Rails (e.g. sinatra):
url = URI.parse(ENV['DATABASE_URL'])
ActiveRecord::Base.establish_connection(
encoding: 'unicode',
pool: 5,
timeout: 5000,
reconnect: true,
adapter: url.scheme,
host: url.host,
database: url.path.sub(%r{^/}, ''),
username: url.user,
password: url.password
)

Related

ActiveRecord connection requires reset?

An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?
For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy

How to setup a MySQL DB with rails in google could SQL

I have a rails app setup on a google cloud instance. I want to have the db in a SQL instance for the extra performance. But I cant see how to do this for a rails app.
I understand you create the SQL instance, start it, install mysql, on it but then how can I have the db and tables added? Creating them manually isn't going to be the solution because normally with rails apps you run rake db:create and rake db:migrate create the DB with tables and columns but this just makes a development.sqlite3 file not a mysql db..
I haven't deployed a rails app before so I think I'm missing something.
Here is my config/databast.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
mysql_settings: &mysql_settings
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
host: 130.211.71.150
database: dbname
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I cant find out what needs to be done to have the db be created and tables and columns migrated into the mysql DB.
In your gem file you can put the sqlite gem under a development/test block and you can add the mysql gem in a production block.
In your database.yml file you can keep the development settings you have currently but then add another setting for production settings. Here you can include your mysql db settings (including the host and port of your SQL instance node)
When you launch your app, you can then launch it locally in development mode to use sqlite for development, but when deploying you can launch in production mode to utilize the mysql specific settings. From there you should be able to use db:create db:migrate etc to connect to that host and setup your Db.
Here is a nice article describes this process.
https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2
As a team, we chose to use mysql for local development as it more closely mimics what your prod environment will be like.

Rails 4: setup Postgresql with environment variables in development after migration from sqlite3

In my Rails 4 app, I used to use the default Sqlite3 in development and test, while using Postgresql in production (Heroku).
I just switched from Sqlite3 to Postgresql for all three environments and everything works fine.
My current database.yml looks like that:
default: &default
adapter: postgresql
encoding: unicode
username: XXX
password: <--- empty --->
database: name-of-my-app
pool: 5
development:
<<: *default
host: localhost
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
host: localhost
production:
<<: *default
host: localhost
As you can see, I am using the default username (my Mac OSX username) and password (no password) and they are hard coded in the database.yml file.
If I understand correctly from what I have read here and there, this is a double bad practice, and I should instead:
define custom username and passwords
do so with environment variables
Is that correct, and what would be the right approach to doing it?
Using your Mac OS X username and no password for development and test is fine. This is how PostgreSQL is set up by default by homebrew, and it is a common configuration for Mac Rails developers.
If you deploy to Heroku, then Heroku will already specify a DATABASE_URL environment variable for you; there is nothing you need to do.
The changes I suggest to your setup are:
You probably don't need to specify a username. If you installed PostgreSQL with homebrew, then your username is the default pg user anyway, so it is redundant.
Specify a different test database. Using the same database name for both dev and test will cause problems.
Completely delete the production section of database.yml. Heroku injects the necessary environment variable, so you don't need it.
With these changes, your database.yml should look like this:
development:
adapter: postgresql
encoding: unicode
database: name_of_my_app_development
min_messages: WARNING
pool: 5
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
database: name_of_my_app_test
min_messages: WARNING
pool: 5

"rails server" using the wrong database

I recently switched a relatively new rails app from sqlite3 to Amazon RDS and configured my database.yml file to use the RDS database in the production environment only.
But now, whenever I try to do any local action on my database (e.g. rails server, rails console, rake db:migrate, etc.) it does that action to the production DB on Amazon's servers rather than my local sqlite3 DB, which is my development DB.
# database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql2
host: mydb.mydbhost.us-east-1.rds.amazonaws.com
reconnect: false
database: mydb
username: myusername
password: mypassword
What am I doing wrong?
UPDATE: Here's my environment.rb file:
# environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Heroku environment variables for local use
heroku_env = File.join(Rails.root, 'config', 'heroku_env.rb')
load(heroku_env) if File.exists?(heroku_env)
# Initialize the rails application
Myapp::Application.initialize!
your not using productions settings
try
rails s -e production
or
RAILS_ENV=production rails s
RAILS_ENV=production rake db:migrate
Figured out the problem after taking a day to get away from it. It's a silly mistake on my part, but I thought I'd post the solution in case someone else encounters a similar issue.
As you can see from my environment.rb file above, I have a heroku_env.rb file, which contains all of my heroku-specific environment variables on my local machine for development purposes. In that file, I declared a ENV['DATABASE_URL'] variable, which links to my Amazon RDS database. Deleting this from the file solved the problem!
Thanks to everyone who offered answers to help!

Heroku Push Errors

Good afternoon,
I browsed the other questions on this topic, and it appears nobody has asked about this issue. I was expecting otherwise!
In any case - I have a PostgreSQL database server running on my local machine with a MYAPP_DEVELOPMENT database. I've tried to do a
heroku db:push
But keep getting the following error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PGError: FATAL: role "brandon" does not exist
This obviously has something to do with the permissions & users on the local/heroku shared database, but I'm honestly not that good with this kind of stuff. Any help would be appreciated. I'm currently including the "database.yml" file in my slug to Heroku, which has all of the login/password info for my local database... hence why I was not expecting this kind of error.
Thanks!
** EDIT **
Here is the contents of my database.yml file (edited for clarity):
common: &common
adapter: postgresql
encoding: unicode
username: user
password: secret
test:
<<: *common
database: myapp_test
development:
<<: *common
database: myapp_development
production:
<<: *common
database: myapp_production
I figure it must be something on the Heroku setup side. Note that nowhere in my database.yaml file does "brandon" show up. I'm not quite sure where it is pulling that from. My database user name is not that (although that is my name haha)
This error generally means a username has not been specified under the appropriate environment in your database.yml file.
Your database.yml file should resemble the following (I use MySQL locally, hence the adapter and socket parameters):
common: &common
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: <your_username>
password: <your_password>
socket: /tmp/mysql.sock
development:
<<: *common
database: appname_dev
test:
...
staging:
...
production:
<<: *common
database: appname_prod
If that doesn't solve your problem, check to ensure Heroku is running in the correct environment:
heroku config --app <appname>
Ensure RACK_ENV is set to production
If not: heroku config:add RACK_ENV=production --app <appname>
(It would have been helpful if you pasted the contents of your database.yml file.)
You will have to use the Heroku specific ENV var to use the DB which is usually ENV['DATABASE_URL']
Edit: If you have a db mapper like datamapper you'd write it like this:
DataMapper.setup(:default, ENV['DATABASE_URL'] || LOCAL_DB_URL)
which then either uses the Heroku DB if it exists or defaults to your local DB which is stored in the LOCAL_DB_URL var.
Thanks for everyone's input to this one. I ended up fixing my issue by explicitly providing my login details to Heroku during the db:push... e.g.:
heroku db:push postgres://postgres:PASSWORD#127.0.0.1:5432/DATABASE
Not sure why it isn't using the (same) information in my database.yml file, but oh well.

Resources