Drop database / database in use question

Поиск
Список
Период
Сортировка
От Dan Armbrust
Тема Drop database / database in use question
Дата
Msg-id 82f04dc40810161535r1a8c951ch46c8af2ee4303837@mail.gmail.com
обсуждение исходный текст
Ответы Re: Drop database / database in use question
Список 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

В списке pgsql-general по дате отправления:

Предыдущее
От: Jeff Davis
Дата:
Сообщение: postgresql on 64-bit windows
Следующее
От: "Craig Ayliffe"
Дата:
Сообщение: Re: Problems with Timezones in Australia