Re: Weird "template1" errors on CREATE DATABASE

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ken Williams <ken(at)mathforum(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Weird "template1" errors on CREATE DATABASE
Date: 2003-04-18 14:40:44
Message-ID: 3892.1050676844@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ken Williams <ken(at)mathforum(dot)org> writes:
> I'm getting some weird errors when I run some Perl code that does a
> CREATE DATABASE query. Sometimes it dies with the error 'ERROR:
> CREATE DATABASE: source database "template1" is being accessed by other
> users'. Other times it works fine.

> The thing is, this server is running on my laptop, and no other code is
> accessing the database except the code I'm running right now.
> Sometimes it happens the very first time I connect to the database
> (using "template1" as advised in the docs) and try to create a
> database. So there couldn't *possibly* be anyone else accessing the
> "template1" database.

I have seen reports like this before, and AFAICT the issue is simply
that your previous connection to template1 hasn't finished cleaning
itself up yet. It takes a finite amount of time for a backend to shut
down. Also, it's not unheard-of for client-side code to be holding a
connection open for longer than the programmer realizes, especially
when the connection closure is triggered indirectly through garbage
collection. Perl is one language where that applies.

Throwing a fractional-second delay in before you try to do the CREATE
DATABASE will probably make the problem go away. Alternatively, fix
your client-side code so that it's not opening a fresh connection for
each SQL command, as I surmise it's doing now.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-04-18 14:48:21 default locale considered harmful? (was Re: Using index for "like 'ABC%'" type query)
Previous Message Andrew Sullivan 2003-04-18 14:31:05 Re: Using index for "like 'ABC%'" type query