views:

442

answers:

2

I have a rails application running over Postgres.

I have two servers: one for testing and the other for production.

Very often I need to clone the production DB on the test server.

The command I'm runnig via Vlad is:

rake RAILS_ENV='test_server' db:drop db:create

The problem I'm having is that I receive the following error:

ActiveRecord::StatementInvalid: PGError: ERROR: database <database_name> is being accessed by other users DROP DATABASE IF EXISTS <database_name>

This happens if someone has accessed the application via web recently (postgres keeps a "session" opened)

Is there any way that I can terminate the sessions on the postgres DB?

Thank you.

Edit

I can delete the database using phppgadmin's interface but not with the rake task.

How can I replicate phppgadmin's drop with a rake task?

+1  A: 

Let your application close the connection when it's done. PostgreSQL doesn't keep connections open , it's the application keeping the connection.

Frank Heikens
I can delete the database using phppgadmin's interface but not with the rake task.How can I replicate phppgadmin's drop with a rake task?
fjuan
Sorry, can't help you there, I have no experience with rake.But, the error indicates another user is still using the database. That's why you can't delete the database, not by rake not by PhpPgAdmin, impossible.From the manual, DROP DATABASE: it cannot be executed while you or anyone else are connected to the target database.
Frank Heikens
Thank you very much
fjuan
+2  A: 

Rails is likely connecting to the database to drop it but when you log in via phppgadmin it is logging in via the template1 or postgres database, thus you are not affected by it.

Joshua D. Drake
How can I force rails to drop the database? Should I define my own rake action using postgres SQL commands?
fjuan