[SOLVED] How can I drop my production database and reset it with *updated* migration files?

I recently changed some old migration files in my repository, pushed them up and now want to ‘rake db:reset’ the database, ‘rake db:drop’-ing it first, since the schema changed. I proceeded to ssh to my server and:

# cd /var/deploy/myapp/web_head/current
# bundle exec rake db:drop
...
PG::ObjectInUse: ERROR:  database "myapp_production" is being accessed by other users

I encountered that error, which indicates that the database cannot be dropped or reset since it is being used in a process launched by another user other than root. So a good solution would be to:

1) Kill the process.

2) Drop/Reset/Migrate/Seed whatever you want to do with the DB.

3) Reconnect the app to the database.(In my case Rails with PostgreSQL)

As suggested by Philip from Cloud66 support(thanks for that!), we can find and kill the process with:

ps xa | grep postgres: | grep $POSTGRESQL_DATABASE | grep -v grep | awk '{print $1}' | sudo xargs kill

Here replace $POSTGRESQL_DATABASE with your database name. And problem solved!

Thanks for adding this! Just a small note - the $POSTGRESQL_DATABASE is an environment variable which will contain the database name, so no need to replace it :)

You must be logged in to answer this question