CREATE DATABASE name
- [ WITH [ LOCATION = 'dbpath' ]
- [ TEMPLATE = template ]
- [ ENCODING = encoding ] ]
+ [ WITH [ OWNER [ = ] dbowner ]
+ [ LOCATION [ = ] 'dbpath' ]
+ [ TEMPLATE [ = ] template ]
+ [ ENCODING [ = ] encoding ] ]
+
+ dbowner
+
+ Name of the database user who will own the new database,
+ or DEFAULT to use the default (namely, the
+ user executing the command).
+
+
+
dbpath
CREATE DATABASE creates a new
- The creator becomes the owner of the new database.
+
+
+ Normally, the creator becomes the owner of the new database.
+ A different owner may be specified by using the
+ clause (but only superusers may create databases on behalf of other users).
+ To create a database owned by oneself, either superuser privilege
+ or CREATEDB privilege is required. A superuser may create a database
+ for another user, even if that user has no special privileges himself.
There is no CREATE DATABASE statement in SQL92.
- Databases are equivalent to catalogs whose creation is implementation-defined.
+ Databases are equivalent to catalogs, whose creation is
+ implementation-defined.
Access privileges on functions
Access privileges on procedural languages
+CREATE DATABASE has OWNER option so superuser can create DB for someone else
Kerberos 5 support now works with Heimdal
]]>
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.82 2002/02/23 20:55:46 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.83 2002/02/24 20:20:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "storage/sinval.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
+#include "utils/lsyscache.h"
#include "utils/syscache.h"
#ifdef MULTIBYTE
*/
void
-createdb(const char *dbname, const char *dbpath,
- const char *dbtemplate, int encoding)
+createdb(const char *dbname, const char *dbowner,
+ const char *dbpath, const char *dbtemplate,
+ int encoding)
{
char *nominal_loc;
char *alt_loc;
Datum new_record[Natts_pg_database];
char new_record_nulls[Natts_pg_database];
Oid dboid;
+ int32 datdba;
+ /* obtain sysid of proposed owner */
+ if (dbowner)
+ datdba = get_usesysid(dbowner); /* will elog if no such user */
+ else
+ datdba = GetUserId();
+
+ /* check permission to create database */
if (!get_user_info(GetUserId(), &use_super, &use_createdb))
elog(ERROR, "current user name is invalid");
- if (!use_createdb && !use_super)
- elog(ERROR, "CREATE DATABASE: permission denied");
+ if (datdba == (int32) GetUserId())
+ {
+ /* creating database for self: can be superuser or createdb */
+ if (!use_createdb && !use_super)
+ elog(ERROR, "CREATE DATABASE: permission denied");
+ }
+ else
+ {
+ /* creating database for someone else: must be superuser */
+ /* note that the someone else need not have any permissions */
+ if (!use_super)
+ elog(ERROR, "CREATE DATABASE: permission denied");
+ }
/* don't call this in a transaction block */
if (IsTransactionBlock())
/* Form tuple */
new_record[Anum_pg_database_datname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(dbname));
- new_record[Anum_pg_database_datdba - 1] = Int32GetDatum(GetUserId());
+ new_record[Anum_pg_database_datdba - 1] = Int32GetDatum(datdba);
new_record[Anum_pg_database_encoding - 1] = Int32GetDatum(encoding);
new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(false);
new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(true);
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.161 2002/02/18 23:11:14 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.162 2002/02/24 20:20:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
if (from->dbname)
newnode->dbname = pstrdup(from->dbname);
+ if (from->dbowner)
+ newnode->dbowner = pstrdup(from->dbowner);
if (from->dbpath)
newnode->dbpath = pstrdup(from->dbpath);
if (from->dbtemplate)
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.109 2002/02/18 23:11:14 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.110 2002/02/24 20:20:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
{
if (!equalstr(a->dbname, b->dbname))
return false;
+ if (!equalstr(a->dbowner, b->dbowner))
+ return false;
if (!equalstr(a->dbpath, b->dbpath))
return false;
if (!equalstr(a->dbtemplate, b->dbtemplate))
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.278 2002/02/18 23:11:17 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.279 2002/02/24 20:20:20 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
%type drop_behavior
%type createdb_opt_list, createdb_opt_item
+%type opt_equal
%type opt_lock, lock_type
%type opt_force, opt_or_replace
/* for now, just make this the same as CREATE DATABASE */
CreatedbStmt *n = makeNode(CreatedbStmt);
n->dbname = $3;
+ n->dbowner = NULL;
n->dbpath = NULL;
n->dbtemplate = NULL;
n->encoding = -1;
n->dbname = $3;
/* set default options */
+ n->dbowner = NULL;
n->dbpath = NULL;
n->dbtemplate = NULL;
n->encoding = -1;
case 3:
n->encoding = lfirsti(lnext(optitem));
break;
+ case 4:
+ n->dbowner = (char *) lsecond(optitem);
+ break;
}
}
$$ = (Node *)n;
{
CreatedbStmt *n = makeNode(CreatedbStmt);
n->dbname = $3;
+ n->dbowner = NULL;
n->dbpath = NULL;
n->dbtemplate = NULL;
n->encoding = -1;
* createdb_opt_item returns 2-element lists, with the first element
* being an integer code to indicate which item was specified.
*/
-createdb_opt_item: LOCATION '=' Sconst
+createdb_opt_item: LOCATION opt_equal Sconst
{
$$ = lconsi(1, makeList1($3));
}
- | LOCATION '=' DEFAULT
+ | LOCATION opt_equal DEFAULT
{
$$ = lconsi(1, makeList1(NULL));
}
- | TEMPLATE '=' name
+ | TEMPLATE opt_equal name
{
$$ = lconsi(2, makeList1($3));
}
- | TEMPLATE '=' DEFAULT
+ | TEMPLATE opt_equal DEFAULT
{
$$ = lconsi(2, makeList1(NULL));
}
- | ENCODING '=' Sconst
+ | ENCODING opt_equal Sconst
{
int encoding;
#ifdef MULTIBYTE
#endif
$$ = lconsi(3, makeListi1(encoding));
}
- | ENCODING '=' Iconst
+ | ENCODING opt_equal Iconst
{
#ifdef MULTIBYTE
if (!pg_get_enconv_by_encoding($3))
#endif
$$ = lconsi(3, makeListi1($3));
}
- | ENCODING '=' DEFAULT
+ | ENCODING opt_equal DEFAULT
{
$$ = lconsi(3, makeListi1(-1));
}
+ | OWNER opt_equal name
+ {
+ $$ = lconsi(4, makeList1($3));
+ }
+ | OWNER opt_equal DEFAULT
+ {
+ $$ = lconsi(4, makeList1(NULL));
+ }
;
+opt_equal: '=' { $$ = TRUE; }
+ | /*EMPTY*/ { $$ = FALSE; }
+ ;
/*****************************************************************************
*
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.125 2002/02/07 00:27:30 inoue Exp $
+ * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.126 2002/02/24 20:20:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
set_ps_display(commandTag = "CREATE DATABASE");
- createdb(stmt->dbname, stmt->dbpath,
- stmt->dbtemplate, stmt->encoding);
+ createdb(stmt->dbname, stmt->dbowner,
+ stmt->dbpath, stmt->dbtemplate,
+ stmt->encoding);
}
break;
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: dbcommands.h,v 1.19 2001/11/05 17:46:33 momjian Exp $
+ * $Id: dbcommands.h,v 1.20 2002/02/24 20:20:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef DBCOMMANDS_H
#define DBCOMMANDS_H
-extern void createdb(const char *dbname, const char *dbpath,
- const char *dbtemplate, int encoding);
+extern void createdb(const char *dbname, const char *dbowner,
+ const char *dbpath, const char *dbtemplate,
+ int encoding);
extern void dropdb(const char *dbname);
#endif /* DBCOMMANDS_H */
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: parsenodes.h,v 1.152 2002/02/18 23:11:41 petere Exp $
+ * $Id: parsenodes.h,v 1.153 2002/02/24 20:20:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
{
NodeTag type;
char *dbname; /* name of database to create */
+ char *dbowner; /* name of owner (NULL = default) */
char *dbpath; /* location of database (NULL = default) */
char *dbtemplate; /* template to use (NULL = default) */
int encoding; /* MULTIBYTE encoding (-1 = use default) */