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 |
2
Answers
I think you don’t have the database
database1_test
You can login to Postgres via:
sudo -u postgres psql
to login into Postgresl
will show the all databaseIf
database1_test
doesn’t exist, run thiscmd
to create and migrate database for testingThe issue might be that your
ENV
variables aren’t available in your test suite.To check this try accessing them in a console:
If I’m right and these
ENV
vars aren’t available when your environment istest
, you’ll need to look at a strategy to include theENV
vars you need.