Thread: functions and creating

functions and creating

From
Alex du Plessis
Date:
Hi List,

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?

Thnx

Alex

Re: functions and creating

From
Michael Fuhr
Date:
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