Ken Williams <ken@mathforum.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