1

I've got a Rails app that works fine in development and testing, but in production, where it is configured to use a remote Postgresql database, it is failing to create its table. The database is setup and working. I can connect to it via psql as the app, from the production server, using the same configuration that I specify in my database.yml, and then I can create and query tables with commands, no problem.

When I execute database-related rake tasks, everything looks good:

$ rake db:reset
-- create_table("users", {:force=>true})
   -> 0.0293s
-- add_index("users", ["username"], {:name=>"index_users_on_username", :unique=>true})
   -> 0.0428s
-- initialize_schema_migrations_table()
   -> 0.0212s
-- create_table("users", {:force=>true})
   -> 0.0097s
-- add_index("users", ["username"], {:name=>"index_users_on_username", :unique=>true})
   -> 0.0058s
-- initialize_schema_migrations_table()
   -> 0.0092s

No errors. But the table has not been created.

I discovered this by investigating the Nginx error log, but the easiest way to reproduce the error is to fire up the console and do anything with with the app's only model:

$ rails console production
Loading production environment (Rails 4.2.0.beta4)
2.1.3 :001 > all_users = User.all
PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 1: SELECT "users".* FROM "users"
                          ^

I've also ssh'd into the database server and inspected the database with \z, and it isn't there.

I think the app is connecting to the database server properly, because earlier I was getting permissions errors, and I resolved those. But I don't know how to make sure. And if I do confirm that I am connecting properly, where do I go from there?

Thanks!

hoverbikes
  • 435
  • 3
  • 12
  • 1
    Is your `db:reset` being run against the same database that you're connecting to? – mu is too short Oct 30 '14 at 23:57
  • 1
    If you have access, check postgresql log file, which you will find in $PGDATA/pg_log/. It is possible that an error is occurring which your application is not reporting. – harmic Oct 31 '14 at 00:02
  • I have full access. I checked it out, and it has basically the same error. So I suppose this confirms that the configuration is OK; that is, the errors are happening on the DB server. There is a slight bit more detail, one line that wasn't showing on the webserver: `2014-10-30 19:03:06 EDTERROR: relation "users" does not exist at character 323` – hoverbikes Oct 31 '14 at 00:23

1 Answers1

2

Following mu's suggestion, I ran rake db:reset, explicitly specifying the environment.

rake db:reset RAILS_ENV=production --trace

This revealed the error at the heart of the problem:

PG::ObjectInUse: ERROR:  database "database_name" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.

I ssh'd to the database server, killed all active connections, and restarted the service.

Back on the web server, I reran the rake task above, and it worked. Thanks mu and harmic!

Community
  • 1
  • 1
hoverbikes
  • 435
  • 3
  • 12