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!
Related
I'm deploying small app from C9 to github then to Heroku!
In heroku deploy dashboard, it says it's been deployed, but when I open the app, it just gives me error msg.
So I searched a lot of Stackoverflow answers and was following the direction from Heroku website(https://devcenter.heroku.com/articles/sqlite3) to remove sqlite3 and install the gem pg.
I did replace gem 'sqlite3' to gem 'pg' and did 'bundle install', and then
It says I need to convert my config/database.yml file, So I replaced it like this:
development:
adapter: postgresql
database: my_database_development
pool: 5
timeout: 5000
test:
adapter: postgresql
database: my_database_test
pool: 5
timeout: 5000
production:
adapter: postgresql
database: my_database_production
pool: 5
timeout: 5000
and then when I come back to c9 bash and type 'rake db:create',
it shows me
**rake aborted!
ActiveRecord::NoDatabaseError: FATAL: database "my_database_development" does not exist**
this error.
someone said 'bundle exec rake db:setup' would work, so I did, and then it shows
**Couldn't create database for {"adapter"=>"postgresql", "database"=>"my_database_test", "pool"=>5, "timeout"=>5000}
/home/ubuntu/workspace/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/ubuntu/workspace/config/application.rb to limit the frameworks that will be loaded.**
I don't know how to react to this msg.... when I type 'rake db:migrate' and it shows this again.
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: database "my_database_development" does not exist
I'm not using database, so maybe I can try something on 'config/application.rb' file, but how should I do??
It wasn't necessary to change your development and test databases, those could still have used sqlite3 and so your C9 instance would have continued to work.
(Although there are some advantages to using a consistent adapter across all environments)
If this is just a hobby exercise I would be tempted to keep sqlite3 for development and test.
Also, Heroku replaces the database.yml with its own version so you don't need a production stanza in database.yml
You do need the pg gem, but you can specify it is only loaded in production and you can specify the sqlite3 gem is only loaded in development and test. Do that in your Gemfile and then bundle.
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
If you want to use PostgreSQL in your C9 instance, you have to install PostgreSQL (the database, not just the adapter). It's not a simple process.
This wiki explains how you would install it.
https://wiki.postgresql.org/wiki/Detailed_installation_guides
one of the solution that you can create database manually this is easily solut
I a little bit new for heroku and postgresql and dont know how works translting from mysql to postgresql.
My application was developed on mysql and to run it from heroku i made some steps:
1. Added a gem 'pg' and gem 'rails_12factor' like that:
group :production do
gem 'pg'
gem 'rails_12factor'
end
And bundle it without production
2. I also changed my database.yml into:
(Also i have question, how can i use mysql in devolepment and postgre in production?)
# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
# gem install pg
# On OS X with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
But!
When i tryed to work via heroku(downloading works fine) my dynamic pages wasnt working, and i get this errors:
So, i guessed that DB just doesnt migrate, OK, i runned via console this:
heroku run rake db:migrate --app name
Aaand i now i have this error:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "schoolings" does not exist
: ALTER TABLE "students" ADD CONSTRAINT "fk_rails_e33c769d03"
FOREIGN KEY ("schooling_id")
REFERENCES "schoolings" ("id")
My questions:
1. Why it happens? Did i miss some steps when deployed my app?
2. How can i separate production and dev DB in deployment.yml?
Thanks, for any help!
First you have to create the database on heroku.
heroku run rake db:create --app-name
Then you can run the migrations.
I'm trying to deploy my first app to Heroku. I'm using SQLite as the database. As far as I know Heroku doesn't use SQLite - it switches to Postgres in the backend.
When I'm deploying I get the following error:
/usr/ruby1.8.7/lib/ruby/gems/1.8/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in
`require': no such file to load --
sqlite3 (LoadError)
My Gemfile (which is what I assume is causing this problem) looks as follows:
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
What am I doing wrong?
Heroku doesn't support SQLite databases. You need to use PostgreSQL on production, as I also explained in this post.
group :production do
gem "pg"
end
group :development, :test do
gem "sqlite3", "~> 1.3.0"
end
Actually, it's recommended to use in development/test an environment as close as possible to production. Therefore, I suggest you to switch all your environments to PostgreSQL.
# replace gem "sqlite3" with
gem "pg"
Simone Carletti is correct and so is Joost. You only need to group the sqlite3 gem or remove it entirely from your Gemfile. Heroku just needs to know that you don't want to use sqlite3 for production
So this:
...
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
...
Or this:
...
#No reference to sqlite3-ruby
...
If you remove the reference entirely you will probably mess up your local db though
After banging my head against this problem, I realized I was pushing the master branch of my repo to heroku, while I was making all of my postgres changes in my deploy-postgres branch of my repo!
I merged my deploy-postgres branch with my local master [git checkout master; git merge deploy-postgres] and then could run git push heroku master as per the heroku documentation.
I was stuck on this for hours looking at every answer here, but I couldn't get enough details to make it come together. This paged walked me through everything. http://railsapps.github.io/rails-heroku-tutorial.html
Good luck.
i was facing similar issue, but I realized that I was on a different branch - new_layout and was pushing master.
So I pushed my desired branch to heroku using following command and everything worked fine.
git push heroku new_layout:master
You can use clearDB addon
and gem 'mysql2' instead of gem 'sqlite3'
I'm using sqlite3 and deploy to Heroku no problem. Here is my database.yml
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
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'm trying to deploy my first app to Heroku. I'm using SQLite as the database. As far as I know Heroku doesn't use SQLite - it switches to Postgres in the backend.
When I'm deploying I get the following error:
/usr/ruby1.8.7/lib/ruby/gems/1.8/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in
`require': no such file to load --
sqlite3 (LoadError)
My Gemfile (which is what I assume is causing this problem) looks as follows:
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
What am I doing wrong?
Heroku doesn't support SQLite databases. You need to use PostgreSQL on production, as I also explained in this post.
group :production do
gem "pg"
end
group :development, :test do
gem "sqlite3", "~> 1.3.0"
end
Actually, it's recommended to use in development/test an environment as close as possible to production. Therefore, I suggest you to switch all your environments to PostgreSQL.
# replace gem "sqlite3" with
gem "pg"
Simone Carletti is correct and so is Joost. You only need to group the sqlite3 gem or remove it entirely from your Gemfile. Heroku just needs to know that you don't want to use sqlite3 for production
So this:
...
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
...
Or this:
...
#No reference to sqlite3-ruby
...
If you remove the reference entirely you will probably mess up your local db though
After banging my head against this problem, I realized I was pushing the master branch of my repo to heroku, while I was making all of my postgres changes in my deploy-postgres branch of my repo!
I merged my deploy-postgres branch with my local master [git checkout master; git merge deploy-postgres] and then could run git push heroku master as per the heroku documentation.
I was stuck on this for hours looking at every answer here, but I couldn't get enough details to make it come together. This paged walked me through everything. http://railsapps.github.io/rails-heroku-tutorial.html
Good luck.
i was facing similar issue, but I realized that I was on a different branch - new_layout and was pushing master.
So I pushed my desired branch to heroku using following command and everything worked fine.
git push heroku new_layout:master
You can use clearDB addon
and gem 'mysql2' instead of gem 'sqlite3'
I'm using sqlite3 and deploy to Heroku no problem. Here is my database.yml
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000