I just launched my Rails app that I am building in Cloud9 to Heroku (woohoo!). My default database was sqlite3, so in the deployment process I added PostgreSQL in my gem as such:
# Use postgresql as the database in production
group :production do
gem 'pg'
end
# Use sqlite3 as the database in development
group :development do
gem 'sqlite3'
end
I want to keep my sqlite3 database in development and use PostgreSQL for production. My question is how do I monitor or run queries for the PostgreSQL data in my production Heroku site.
I'm used to running "rails c" then "User.all" in my terminal to see my users. Now if I run that it doesn't show any data from production. How do I see that data? I did some searching and i think I should be using "psql" command. But if I run "psql -l" for example I get:
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
If you just want to run some PostgreSQL queries on Heroku production then type following command in your terminal:
heroku pg:psql (or heroku pg:psql -a your-heroku-app-name)
Here you will be able to run such queries like select * from users. To exit the terminal type \q.
You can also invoke Heroku production console with
heroku run console (or heroku run console -a your-heroku-app-name)
to run such queries like User.all.
Related
I need to create a new test database (postgres) and am having trouble. It seems that when I try to run any tests it is attempting to connect to the production database which is worrisome for many reasons. Here is my database.yml currently.
test:
adapter: postgresql
encoding: unicode
database: xxxxxx-test
pool: 5
username: xxx
host: localhost
But when I run a test I see:
An error occurred while loading ./spec/models/recipe_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
PG::ConnectionBad:
FATAL: no pg_hba.conf entry for host "IP_ADDRESS", user "PRODUCTION_USER", database "PRODUCTION_DATABASE", SSL off
I'm not sure why this is happening or how I am supposed to set this up.
Here's my gemfile for test:
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15', '< 4.0'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
gem 'rspec-rails', '~> 3.5'
gem 'database_cleaner'
gem 'factory_bot_rails'
end
It's worth noting that I do not have database_cleaner doing anything at the moment.
The database configuration from database.yml is merged with ENV["DATABASE_URL"]. But ENV["DATABASE_URL"] ALWAYS takes precedence over the YML configuration. See the Rails guides on configuration.
The bad news is that you have set ENV["DATABASE_URL"] to point to your production database which could have been catastrophic.
The good news is that you seem to have a IP whitelist on the production DB that denied your local IP. Otherwise you would be clobbering your production DB!
To fix this you need to determine where ENV["DATABASE_URL"] is being set and get rid of it. Depending on your setup this can be anywhere from your ~/.profile to the docker container configuration. Then confirm that you have the correct config:
$ rails runner -e test "puts ActiveRecord::Base.connection_config"
This should print something like:
{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"xxxxxx-test"}
At this point its safe to create the databases for dev/test with:
$ rails db:create
$ rails db:schema:load
You can use ENV["DATABASE_URL"] to let developers use their own local configuration, but you should NEVER let it point to the production DB on anything except the actual production server!
I know this is probably dumb question but...
Simply put, I have an app with a database of hundreds of records in development mode. When I push to production and deploy onto the internet, will I lose my database and have to redo it all in production mode?
Just being safe!
Sort of, you won't lose the data itself, that's stored in a database you configured for the development environment, but your production environment likely will have configured another database, which will be empty.
You could copy the database from the development environment and configure rails to use that in production. Depends a bit on what kind of database you use: mysql, sqlite, etc.
Your production database is not pushed. An empty database with your schemas will be created when you run rake exec db:migrate on your production server.
If you want to automate adding your development database records to your production database, there is a gem called yaml_db. It is easy to use and works on MySQL and PostgreSQl. https://github.com/yamldb/yaml_db.
In gemfile:
gem 'yaml_db'
Then, in your console
$ bundle install
rake db:data:dump -> Dump contents of Rails database to db/data.yml
rake db:data:load -> Load contents of db/data.yml into the database
Take a look at the spec for all details.
EDITED: Addition
RAILS_ENV=development bundle exec rake db:data:dump
RAILS_ENV=production bundle exec rake db:data:load
I'm having trouble changing an existing app with SQLite3 to postgreSQL. I'm following this tutorial to convert SQLite3 to postgreSQL and deploy it to heroku: https://devcenter.heroku.com/articles/sqlite3#running-rails-on-postgres.
I removed gem 'sqlite3' and replaced with gem 'pg'.
After modifying config/database.yml, I ran migration.
$rake db:create and $rake db:migrate resulted this error:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Gemfile used in production:
gem 'rails_12factor'
gem 'thin'
gem 'pg'
Following is the errors I get when I run with the local server:
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `initialize': could not connect to server: No such file or directory (PG::ConnectionBad)
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
It looks like you have not added a Heroku Postgres database to your Heroku account. When a database is configured, your application will connect to it. Without a database configured, it's falling back to looking for one locally.
If you do have a database configured, something is wrong with your Heroku environment preventing your database connection settings from being found.
I would not ask this if my peculiar case had already been answered on this forum so please pardon my re-asking.
I have a rails app which uses an sqlite3 database for development and it already contains all the data I need for the app. On pushing this app to heroku I was unable to view the app over there until I had to run heroku run rake db:migrate. Now I can view the app, but I can't login in with admin accounts or do any other thing that requires data from the database. i already did all that is required (I think) as shown in my gem file below:
source 'https://rubygems.org'
gem 'rails', '3.2.12'
gem 'bcrypt-ruby'
gem 'kaminari'
gem 'gon'
.
.
.
gem 'jquery-rails', '2.0.2'
group :development, :test do
gem 'sqlite3', '1.3.7'
end
group :production do
gem 'pg', '0.12.2'
end
.
.
I also tried heroku db:push but that didn't help either. The main issue is that i am working on this app with someone else and we employ github for version control. But whenever I we get updates form each other our sqlite database always seems to empty itself. So to solve this problem we usually have to email the updated database directly to the other, who then proceeds to use it and replace the empty on in his local repo. I suspect that the reason why heroku rakedb:migrate is this.
But giving as we've come this far in our development, we really cannot afford to start over. Is there a way we can populate the remote heroku database with the content of our local repo db? Thanks for your responses
You should export your data from sqlite into a sql dump file and start using postgres locally. If you continue to use sqlite locally and postgres remotely, you will have a bad time.
After you've migrated locally, follow this guide on Importing and Exporting Heroku Postgres Databases with PG Backups.
Also, don't use db:push or db:pull; they are deprecated.
Update
To get the sql file in your local postgres, first install and setup postgres (Homebrew, macports, other...) then do something like so:
$ psql -U user -d database < file.sql
From there, you can use the pg_dump utility to export to a .dump file. You can also import the sql directly to Heroku Postgres:
# this will delete ALL data in your postgres, so make sure it's what you want
$ heroku pg:reset HEROKU_POSTGRESQL_COLOR --app app_name
$ heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql
I am currently making my way through the Ruby on Rails tutorial over at http://ruby.railstutorial.org/ and I am trying to migrate the demo_app database to heroku.
heroku rake db:migrate
rake aborted!
unable to open database file
I have read on other stackoverflow posts that some people fixed this by entering
group :production, :staging do
gem "pg"
end
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
in the gemfile. I also entered it into my gemfile and then deleted my old gemfile.lock and redid my bundle install AND rake db:migrate command. I am still receiving the same error.
I am obviously brand new to ruby, rails and heroku but I understand that the problem seems to be that I am using sqlite locally and postgresql in production (on heroku). Do I now have to install this postgresql onto my machine and then RE-migrate the DB? I am afraid I will not be able to get much more out of the tutorials (or ruby on rails itself) if I cannot use heroku.
Kill it!
I was having the same problem and found no solutions. I think something we are doing in those tutorials is leading us to mangle the database.yml file that heroku generates.
I ended up destroying my heroku app
heroku destroy
and then creating a new one, pushing a fresh copy, and running
heroku create
git push heroku master
heroku rakedb:migrate
This time everything worked fine! Just make sure you have the pg gem in your gemfile for production
group :production do
gem "pg"
end
and add config/database.yml to your .gitignore file too for good measure.
or if it's working ok locally do a heroku db:push to magically put your local sqlite DB into Heroku's postgresql db.
I always work with the same DB platform locally just so I don't run into any differences (usually only when you start doing DB specific SQL) so I run Postgresql locally too.
Had the same problem... with Heroku interface... ran:
heroku rake db:migrate --trace
and found the problem to be with faker, not being found...Since 'faker' in our Gemfile is loaded in the development group, I loaded it in the production group as well.
saved Gemfile
bundle install
git add .
git commit -m "fixed faker"
git push
git heroku push
heroku rake db:migrate
heroku rake db:populate
now everything works...the QUESTION, now is what to do with 100 users on my production site?
At least I can continue with Hartl's tutorial!!