Thread: Droping a database even if someone is connected

Droping a database even if someone is connected

From
Eric Audet
Date:

Is there a way to drop a database even if someone is connected to it?

I use a perl script that
1) drop the database, (dropdb)
2) create a new database, (createdb)
3) restore a database from a dump (psql databasename <dumpfile)

When the drop does not work, everything else fails!

Thanks in advance
Eric

Re: Droping a database even if someone is connected

From
Oliver Elphick
Date:
On Thu, 2003-03-13 at 19:19, Eric Audet wrote:
> Is there a way to drop a database even if someone is connected to it?
>
> I use a perl script that
> 1) drop the database, (dropdb)
> 2) create a new database, (createdb)
> 3) restore a database from a dump (psql databasename <dumpfile)
>
> When the drop does not work, everything else fails!

If 1) fails, identify the relevant backend process and kill it. Then try
again.

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Bless them which persecute you; bless, and curse not.
      Rejoice with them that do rejoice, and weep with them
      that weep. Recompense to no man evil for evil. Provide
      things honest in the sight of all men."
                                  Romans 12:14,15,17


Re: Droping a database even if someone is connected

From
Tom Lane
Date:
Oliver Elphick <olly@lfix.co.uk> writes:
> On Thu, 2003-03-13 at 19:19, Eric Audet wrote:
>> Is there a way to drop a database even if someone is connected to it?
>>
>> I use a perl script that
>> 1) drop the database, (dropdb)
>> 2) create a new database, (createdb)
>> 3) restore a database from a dump (psql databasename <dumpfile)
>>
>> When the drop does not work, everything else fails!

> If 1) fails, identify the relevant backend process and kill it. Then try
> again.

Actually, what's the need to drop the database at all?  Running pg_dump
with -c option would allow the dump to be loaded into the existing DB.

            regards, tom lane