Re: functions and creating - Mailing list pgsql-novice

From Michael Fuhr
Subject Re: functions and creating
Date
Msg-id 20060803224432.GA18273@winnie.fuhr.org
Whole thread Raw
In response to functions and creating  (Alex du Plessis <alexdup01@telkomsa.net>)
List pgsql-novice
On Thu, Aug 03, 2006 at 09:08:02PM +0200, Alex du Plessis wrote:
> I need to create new databases on demand from my app.  Due to system
> constraints I cannot create the database from the app itself and was
> thinking of using a pl/psql function within the master database to
> perform this duty.
>
> However, it seems I cannot supply the name of the new database to the
> function via a function parameter.  Is there a way to do this and am
> just being ignorant?.  Does anybody know how to get around this one?

In some cases like this you can use PL/pgSQL's EXECUTE statement
to execute a dynamically-created statement; unfortunately this isn't
one of those cases because you can't execute CREATE DATABASE from
within a function.  If you try you'll get the following error:

  ERROR:  CREATE DATABASE cannot be executed from a function

One way for a function to execute forbidden commands is for the
function to use contrib/dblink to connect to the database and execute
the commands over that connection.

--
Michael Fuhr

pgsql-novice by date:

Previous
From: "Garcia, Joshua"
Date:
Subject: drop trigger all
Next
From: Ramon Orticio
Date:
Subject: Re: updating postgres