Re: functions and creating

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Alex du Plessis <alexdup01(at)telkomsa(dot)net>
Cc: Postgres novice mailing list <pgsql-novice(at)postgresql(dot)org>
Subject: Re: functions and creating
Date: 2006-08-03 22:44:32
Message-ID: 20060803224432.GA18273@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Ramon Orticio 2006-08-04 01:26:38 Re: updating postgres
Previous Message Garcia, Joshua 2006-08-03 20:38:52 drop trigger all