trosborn
trosborn

Reputation: 1668

Error: Relation does not exist when running Rails in production using postgresql

I'm having a problem with my application on Heroku, and in order to determine whether it is specific to my production environment or specific to Heroku itself I am running my production environment locally on my machine with rails s -e production

When I try to load my posts index, I get the following error:

PG::UndefinedTable: ERROR:  relation "posts" does not exist
LINE 1: SELECT "posts".* FROM "posts"
                          ^
: SELECT "posts".* FROM "posts"
PG::UndefinedTable: ERROR:  relation "posts" does not exist
LINE 1: SELECT "posts".* FROM "posts"

What's weird is my posts index works fine on Heroku, as well as in development and in test. When I run bundle exec rake db:create:all I get the follow output:

project_development already exists
project_test already exists
project_production already exists

My schema is as follows:

create_table "posts", force: true do |t|
  t.string   "title"
  t.text     "body"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "author_id"
  t.boolean  "published"
end

When I fire up rails db in the command line, I can see the table as expected when I run \dt:

 public | posts             | table | Thomas

However when I fire it up with ENV=production and run \dt I get the following:

Schema |       Name        | Type  | Owner  
--------+-------------------+-------+--------
public | schema_migrations | table | Thomas

I've tried running ENV=production rake db:schema:load and that does not solve the problem.

For reference, here is my database.yaml:

development:
  adapter: postgresql
  encoding: utf8
  database: project_development
  pool: 5
  username:
  password:

test:
  adapter: postgresql
  encoding: utf8
  database: project_test
  pool: 5
  username:
  password:

production:
  adapter: postgresql
  encoding: utf8
  database: project_production
  pool: 5
  username:
  password:

Upvotes: 2

Views: 2852

Answers (1)

Prakash Murthy
Prakash Murthy

Reputation: 13077

The production database created from the database.yml file with rake db:create:all is created locally on your machine, not on heroku.

Heroku automatically infers the database config for the production environment, and creates it when you run git push heroku master for the first time.

The error message about posts table not existing is because of not running migrations on heroku. Run heroku run rake db:migrate to apply the migrations to the production database on heroku.

Also, you don't need to have the production section of the database.yml on your local environment.

Do refer to https://devcenter.heroku.com/articles/getting-started-with-rails4 while working for the first time with heroku; it is comprehensive and very helpful.

EDIT

The actual problem was to get the local production database to work.

Solution was to apply the migrations correctly with rake db:migrate RAILS_ENV=production

Upvotes: 2

Related Questions