Drop database / database in use question

From: "Dan Armbrust" <daniel(dot)armbrust(dot)list(at)gmail(dot)com>
To: "pgsql general" <pgsql-general(at)postgresql(dot)org>
Subject: Drop database / database in use question
Date: 2008-10-16 22:35:13
Message-ID: 82f04dc40810161535r1a8c951ch46c8af2ee4303837@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have some installation tools which in some cases need to replace a
postgres database.

For this example, assume my database name is 'fred'.

Also, assume that no other processes are accessing this database. Any
database access will come from my running installer.

First, after validating some passwords, my installer will run this command:

psql -c "select datname from pg_stat_activity where datname='fred'" -U
username -d template1

I then scan the output looking for 'fred'. My (perhaps incorrect)
assumption is that if I don't find the database name 'fred' in the
output, then I can assume that no process is using this database, and
I should be able to drop it.

Later, my installer runs this code:

psql -c "drop database fred" -U username -d template1

99% of the time, everything works fine.

1% of the time, the drop fails, and I get this:

ERROR: database "fred" is being accessed by other users

My two part question is why, and what can I do about it? At a
minimum, I need to be able to reliably determine if I can drop the
database. Ideally, I would like to be able to drop the database even
though it is still in use - force an override of some sort - kick out
the offending user.

Thanks for any advice....

Dan

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ayliffe 2008-10-16 22:52:03 Re: Problems with Timezones in Australia
Previous Message Jeff Davis 2008-10-16 21:56:25 postgresql on 64-bit windows