I am running tests using rails test:models and got this error (though the tests still run)
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
A database called postgres does not exist because I gave the DB's a different name.
My database.yml is :
# config/database.yml
default: &default
adapter: postgresql
encoding: unicode
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
pool: 5
timeout: 5000
host: <%= ENV['POSTGRES_HOST'] %>
development:
<<: *default
database: <%= ENV['POSTGRES_DB'] %>
test:
<<: *default
database: <%= ENV['POSTGRES_TEST_DB'] %>
production:
<<: *default
database: <%= ENV['POSTGRES_DB'] %>
and my respective .env is :
# ./env
POSTGRES_USER='bob'
# If you declared a password when creating the database:
POSTGRES_HOST='localhost'
POSTGRES_DB='database1'
POSTGRES_TEST_DB='database1_test'
In PSQL, you can also see the test database isn't using the .env value for its owner but the production database is :
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+---------------+----------+---------+-------+---------------------------------
database1 | bob | UTF8 | C | C |
database1_test | bobsurname | UTF8 | C | C |
I think you don't have the database database1_test
You can login to Postgres via:
sudo -u postgres psql to login into Postgres
\l will show the all database
If database1_test doesn't exist, run this cmd to create and migrate database for testing
RAILS_ENV=test rails db:prepare
The issue might be that your ENV variables aren't available in your test suite.
To check this try accessing them in a console:
> RAILS_ENV=test rails console
> ENV # should print out all your ENV variables
> ENV['POSTGRES_TEST_DB'] # should print out the name of your test db
If I'm right and these ENV vars aren't available when your environment is test, you'll need to look at a strategy to include the ENV vars you need.
ThoughtBot has a few ideas that are pretty solid
You can add ENV vars manually as this SO from way back points out
Related
I recently switched db's from sqlite3 to PG. My username and password for pg were hard-coded, and although it was working, this was not safe practice. SO I stored my password in an ENV variable in a .yml file, and I reference that variable in my database.yml file, but when I run the server, it gives me the error "PG::ConnectionBad
fe_sendauth: no password supplied"
the following is my pg_keys.yml file
PG_PASSWORD: "***********"
And the following is my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: stockapp_development
username: postgres
password: <%= ENV['PG_PASSWORD'] %>
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
database: stockapp_test
host: localhost
username: postgres
password: <%= ENV['PG_PASSWORD'] %>
production:
<<: *default
database: stockapp_production
username: stockapp
password: <%= ENV['STOCKAPP_DATABASE_PASSWORD'] %>
Why is it saying that no password is supplied? Does my database.yml not see the password in the other file? Do I need to export the password?
It is because ENV variables come from https://github.com/bkeepers/dotenv.
Install this gem by adding it to your Gemfile, then run bundle and set the variable PG_PASSWORD=*********** in .env in your root. Then it should work fine.
have been pulling my hair due this issue :
When I run my rails application on my mac it seems the host is mistaken as database name and it only happens on development environment.
This is my database.yml :
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['DATABASE_USERNAME'] || 'admin' %>
password: <%= ENV['DATABASE_PASSWORD'] || 'password'%>
host: <%= ENV['DATABASE_URL'] || 'localhost'%>
development:
<<: *default
database: cid_dev
test:
<<: *default
database: cid_test
production:
<<: *default
database: cid_api
Then when I run :
$ bundle exec rake db:create
It returns me this :
Database 'localhost' already exists
anyone has any idea what happen in my local environment ?
FYI, I tried rbenv and rvm both has same issue.
Thank you.
I found the answer myself.
Rails using DATABASE_URL environment variable as a place to put connection string. So when I put database host inside env variable DATABASE_URL it will use it as default
https://github.com/rails/rails/blob/fb764ba63e53b728873075a0d207b993409798a2/railties/lib/rails/application/configuration.rb#L88-L102
So to fix it what I need is rename DATABASE_URL to DATABASE_HOST which is the correct one anyway. Thank you
I first noticed trouble after I dropped an Active Record table using this migration:
class DropDelayedJobTables < ActiveRecord::Migration
def change
drop_table :delayed_jobs
end
end
bundle exec rake db:migrate seemed to successfully drop the table; it disappeared from schema following rake db:schema:dump; and the records no longer seemed to exist via the heroku console. But, the table still exists in my postgres production database!
$ psql myapp_production
myapp_production=# \dt
Schema | Name | Type | Owner
--------+-------------------+-------+-----------
public | delayed_jobs | table | leoebrown
public | foods | table | leoebrown
public | lists | table | leoebrown
public | quantities | table | leoebrown
public | schema_migrations | table | leoebrown
public | users | table | leoebrown
I also noticed that when I delete records via heroku console (Food.find(1234).destroy), the records still exist in the myapp_production postgres database. For example, when I check the number of foods in the postgres database, it is:
myapp_production=# SELECT COUNT(*) from FOODS;
count
-------
6716
(1 row)
But when I run Food.count in the heroku console, the result is
In general, my postgres database seems disconnected from my live heroku app.
$heroku run console
irb(main):001:0> Food.count
=> 6161
When I check the number of foods in the live website, it is 6161.
Clearly, I am doing something fundamentally wrong. The production database seems totally disconnected from the app. I didn't even notice, because it wasn't affecting performance. It's almost as though it doesn't matter what is in the production database. Though surely it does matter.
Below is my database.yml file in case it provides a clue. For full disclosure, I'm not sure if/where some of these ENV variables are stored or, if they do exist, where to find them, and what they should be set to.
Thank you for any guidance you may provide.
database.yml:
development:
host: localhost
adapter: postgresql
encoding: UTF8
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
database: myapp_development
test:
host: localhost
adapter: postgresql
encoding: UTF8
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
database: myapp_test
production:
host: <%= ENV['IP'] %>
adapter: postgresql
encoding: UTF8
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
database: myapp_production
Well from your post, I think you are saying that your local database named my_app_production is not synced with your heroku database. which will never happen.
You should note that to login to psql session of heroku you need to use following command
heroku pg:psql --app <your app_name here>
psql myapp_production lets you open psql session with your local database. It is not your heroku production database.
More on this https://devcenter.heroku.com/articles/heroku-postgresql
I have a Rails(4.1.0) app that works fine on Heroku. However, on my local machine, rake db:migrate fails due to a table for devise that uses inet datatype and I am using sqlite3 for testing.
I have included the postgres gem as well as postgres_ext but still coming up with the error:
undefined method `inet' for #<ActiveRecord::ConnectionAdapters::Table:0x00000005fae9e8>/home/app/db/migrate/20141107192501_add_devise_to_users.rb:19:in `block in up'
If testing locally using Postgres is acceptable just setup the correct adapters. A sample database.yml:
common: &common
adapter: postgresql
encoding: utf8
template: template0 # Required for UTF8 encoding
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
host: <%= ENV["POSTGRES_HOST"] %>
development:
<<: *common
database: 'my_app_dev'
# 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:
<<: *common
database: 'my_app_test'
I'm trying to use Travis Continuous Integration on a Rails project. The documentation says that the test db must be configured as following for SQLite3:
test:
adapter: sqlite3
database: ":memory:"
timeout: 500
But I'd like to keep my default configuration for local tests. Is it possible to keep both my local settings and the Travis requirements?
My solution for this problem is fully based on a blog post with a few differences:
Travis CI specific settings in config/database.travis.yml;
cp config/database.travis.yml config/database.yml in before script section of .travis.yml;
I don't have config/database.yml in source tree.
Here is full listing for both files:
# .travis.yml
language: ruby
rvm:
- 1.9.3
env:
- DB=sqlite
- DB=mysql
- DB=postgresql
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec rake db:test:prepare
- bundle exec rake
before_script:
- cp config/database.travis.yml config/database.yml
- mysql -e 'create database strano_test'
- psql -c 'create database strano_test' -U postgres
# config/database.travis.yml
sqlite: &sqlite
adapter: sqlite3
database: db/<%= Rails.env %>.sqlite3
mysql: &mysql
adapter: mysql2
username: root
password:
database: strano_<%= Rails.env %>
postgresql: &postgresql
adapter: postgresql
username: postgres
password:
database: strano_<%= Rails.env %>
min_messages: ERROR
defaults: &defaults
pool: 5
timeout: 5000
host: localhost
<<: *<%= ENV['DB'] || "postgresql" %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
#mrm's blog post doesn't say anything about answering your question.
I faced the same problem where my postgreql credentials are different on my local machine than travis default. This is the simplest solution I came up with:
# config/database.yml
test:
adapter: postgresql
database: medscraper_test
username: <%= ENV['TRAVIS'] ? 'postgres' : 'MY_TEST_USERNAME' %>
password: <%= ENV['TRAVIS'] ? '' : 'MY_TEST_PASSWORD' %>
Note that Travis CI automatically sets TRAVIS environment variable.
Your solution would be:
# config/database.yml
test:
adapter: sqlite3
database: <%= ENV['TRAVIS'] ? '":memory:"' : 'db/test.sqlite3' %>
timeout: 500
I just wrote a blog post describing how to do this.