Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita833c44

Browse files
committed
Add OWNER option to CREATE DATABASE, so superusers can create databases
on behalf of unprivileged users. Also, make '=' optional in CREATEDATABASE syntax. From Gavin Sherry, with kibitzing and docs by Tom Lane.
1 parentf66f7a5 commita833c44

File tree

9 files changed

+97
-30
lines changed

9 files changed

+97
-30
lines changed

‎doc/src/sgml/ref/create_database.sgml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_database.sgml,v 1.23 2002/01/20 22:19:56 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_database.sgml,v 1.24 2002/02/24 20:20:18 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -24,9 +24,10 @@ PostgreSQL documentation
2424
</refsynopsisdivinfo>
2525
<synopsis>
2626
CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
27-
[ WITH [ LOCATION = '<replaceable class="parameter">dbpath</replaceable>' ]
28-
[ TEMPLATE = <replaceable class="parameter">template</replaceable> ]
29-
[ ENCODING = <replaceable class="parameter">encoding</replaceable> ] ]
27+
[ WITH [ OWNER [ = ] <replaceable class="parameter">dbowner</replaceable> ]
28+
[ LOCATION [ = ] '<replaceable class="parameter">dbpath</replaceable>' ]
29+
[ TEMPLATE [ = ] <replaceable class="parameter">template</replaceable> ]
30+
[ ENCODING [ = ] <replaceable class="parameter">encoding</replaceable> ] ]
3031
</synopsis>
3132

3233
<refsect2 id="R2-SQL-CREATEDATABASE-1">
@@ -47,6 +48,16 @@ CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
4748
</para>
4849
</listitem>
4950
</varlistentry>
51+
<varlistentry>
52+
<term><replaceable class="parameter">dbowner</replaceable></term>
53+
<listitem>
54+
<para>
55+
Name of the database user who will own the new database,
56+
or <literal>DEFAULT</literal> to use the default (namely, the
57+
user executing the command).
58+
</para>
59+
</listitem>
60+
</varlistentry>
5061
<varlistentry>
5162
<term><replaceable class="parameter">dbpath</replaceable></term>
5263
<listitem>
@@ -171,7 +182,15 @@ CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
171182
<para>
172183
<command>CREATE DATABASE</command> creates a new
173184
<productname>PostgreSQL</productname> database.
174-
The creator becomes the owner of the new database.
185+
</para>
186+
187+
<para>
188+
Normally, the creator becomes the owner of the new database.
189+
A different owner may be specified by using the <option>OWNER</>
190+
clause (but only superusers may create databases on behalf of other users).
191+
To create a database owned by oneself, either superuser privilege
192+
or CREATEDB privilege is required. A superuser may create a database
193+
for another user, even if that user has no special privileges himself.
175194
</para>
176195

177196
<para>
@@ -327,7 +346,8 @@ Type: \copyright for distribution terms
327346
</title>
328347
<para>
329348
There is no <command>CREATE DATABASE</command> statement in SQL92.
330-
Databases are equivalent to catalogs whose creation is implementation-defined.
349+
Databases are equivalent to catalogs, whose creation is
350+
implementation-defined.
331351
</para>
332352
</refsect2>
333353
</refsect1>

‎doc/src/sgml/release.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.117 2002/02/23 04:17:45 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.118 2002/02/24 20:20:19 tgl Exp $
33
-->
44

55
<appendix id="release">
@@ -26,6 +26,7 @@ worries about funny characters.
2626
<literallayout><![CDATA[
2727
Access privileges on functions
2828
Access privileges on procedural languages
29+
CREATE DATABASE has OWNER option so superuser can create DB for someone else
2930
Kerberos 5 support now works with Heimdal
3031
]]></literallayout>
3132

‎src/backend/commands/dbcommands.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.82 2002/02/23 20:55:46 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.83 2002/02/24 20:20:19 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -34,6 +34,7 @@
3434
#include"storage/sinval.h"
3535
#include"utils/builtins.h"
3636
#include"utils/fmgroids.h"
37+
#include"utils/lsyscache.h"
3738
#include"utils/syscache.h"
3839

3940
#ifdefMULTIBYTE
@@ -55,8 +56,9 @@ static bool remove_dbdirs(const char *real_loc, const char *altloc);
5556
*/
5657

5758
void
58-
createdb(constchar*dbname,constchar*dbpath,
59-
constchar*dbtemplate,intencoding)
59+
createdb(constchar*dbname,constchar*dbowner,
60+
constchar*dbpath,constchar*dbtemplate,
61+
intencoding)
6062
{
6163
char*nominal_loc;
6264
char*alt_loc;
@@ -79,12 +81,31 @@ createdb(const char *dbname, const char *dbpath,
7981
Datumnew_record[Natts_pg_database];
8082
charnew_record_nulls[Natts_pg_database];
8183
Oiddboid;
84+
int32datdba;
8285

86+
/* obtain sysid of proposed owner */
87+
if (dbowner)
88+
datdba=get_usesysid(dbowner);/* will elog if no such user */
89+
else
90+
datdba=GetUserId();
91+
92+
/* check permission to create database */
8393
if (!get_user_info(GetUserId(),&use_super,&use_createdb))
8494
elog(ERROR,"current user name is invalid");
8595

86-
if (!use_createdb&& !use_super)
87-
elog(ERROR,"CREATE DATABASE: permission denied");
96+
if (datdba== (int32)GetUserId())
97+
{
98+
/* creating database for self: can be superuser or createdb */
99+
if (!use_createdb&& !use_super)
100+
elog(ERROR,"CREATE DATABASE: permission denied");
101+
}
102+
else
103+
{
104+
/* creating database for someone else: must be superuser */
105+
/* note that the someone else need not have any permissions */
106+
if (!use_super)
107+
elog(ERROR,"CREATE DATABASE: permission denied");
108+
}
88109

89110
/* don't call this in a transaction block */
90111
if (IsTransactionBlock())
@@ -254,7 +275,7 @@ createdb(const char *dbname, const char *dbpath,
254275
/* Form tuple */
255276
new_record[Anum_pg_database_datname-1]=
256277
DirectFunctionCall1(namein,CStringGetDatum(dbname));
257-
new_record[Anum_pg_database_datdba-1]=Int32GetDatum(GetUserId());
278+
new_record[Anum_pg_database_datdba-1]=Int32GetDatum(datdba);
258279
new_record[Anum_pg_database_encoding-1]=Int32GetDatum(encoding);
259280
new_record[Anum_pg_database_datistemplate-1]=BoolGetDatum(false);
260281
new_record[Anum_pg_database_datallowconn-1]=BoolGetDatum(true);

‎src/backend/nodes/copyfuncs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.161 2002/02/18 23:11:14 petere Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.162 2002/02/24 20:20:20 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2231,6 +2231,8 @@ _copyCreatedbStmt(CreatedbStmt *from)
22312231

22322232
if (from->dbname)
22332233
newnode->dbname=pstrdup(from->dbname);
2234+
if (from->dbowner)
2235+
newnode->dbowner=pstrdup(from->dbowner);
22342236
if (from->dbpath)
22352237
newnode->dbpath=pstrdup(from->dbpath);
22362238
if (from->dbtemplate)

‎src/backend/nodes/equalfuncs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
2222
* IDENTIFICATION
23-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.109 2002/02/18 23:11:14 petere Exp $
23+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.110 2002/02/24 20:20:20 tgl Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -1099,6 +1099,8 @@ _equalCreatedbStmt(CreatedbStmt *a, CreatedbStmt *b)
10991099
{
11001100
if (!equalstr(a->dbname,b->dbname))
11011101
return false;
1102+
if (!equalstr(a->dbowner,b->dbowner))
1103+
return false;
11021104
if (!equalstr(a->dbpath,b->dbpath))
11031105
return false;
11041106
if (!equalstr(a->dbtemplate,b->dbtemplate))

‎src/backend/parser/gram.y

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.278 2002/02/18 23:11:17 petere Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.279 2002/02/24 20:20:20 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHORDATEMAJOR EVENT
@@ -153,6 +153,7 @@ static void doNegateFloat(Value *v);
153153
%type<ival>drop_behavior
154154

155155
%type<list>createdb_opt_list,createdb_opt_item
156+
%type<boolean>opt_equal
156157

157158
%type<ival>opt_lock,lock_type
158159
%type<boolean>opt_force,opt_or_replace
@@ -733,6 +734,7 @@ CreateSchemaStmt: CREATE SCHEMA UserId
733734
/* for now, just make this the same as CREATE DATABASE*/
734735
CreatedbStmt *n = makeNode(CreatedbStmt);
735736
n->dbname =$3;
737+
n->dbowner =NULL;
736738
n->dbpath =NULL;
737739
n->dbtemplate =NULL;
738740
n->encoding = -1;
@@ -3049,6 +3051,7 @@ CreatedbStmt: CREATE DATABASE database_name WITH createdb_opt_list
30493051

30503052
n->dbname =$3;
30513053
/* set default options*/
3054+
n->dbowner =NULL;
30523055
n->dbpath =NULL;
30533056
n->dbtemplate =NULL;
30543057
n->encoding = -1;
@@ -3068,6 +3071,9 @@ CreatedbStmt: CREATE DATABASE database_name WITH createdb_opt_list
30683071
case3:
30693072
n->encoding =lfirsti(lnext(optitem));
30703073
break;
3074+
case4:
3075+
n->dbowner = (char *)lsecond(optitem);
3076+
break;
30713077
}
30723078
}
30733079
$$ = (Node *)n;
@@ -3076,6 +3082,7 @@ CreatedbStmt: CREATE DATABASE database_name WITH createdb_opt_list
30763082
{
30773083
CreatedbStmt *n = makeNode(CreatedbStmt);
30783084
n->dbname =$3;
3085+
n->dbowner =NULL;
30793086
n->dbpath =NULL;
30803087
n->dbtemplate =NULL;
30813088
n->encoding = -1;
@@ -3093,23 +3100,23 @@ createdb_opt_list: createdb_opt_item
30933100
* createdb_opt_item returns 2-element lists, with the first element
30943101
* being an integer code to indicate which item was specified.
30953102
*/
3096-
createdb_opt_item:LOCATION'='Sconst
3103+
createdb_opt_item:LOCATIONopt_equalSconst
30973104
{
30983105
$$ = lconsi(1, makeList1($3));
30993106
}
3100-
|LOCATION'='DEFAULT
3107+
|LOCATIONopt_equalDEFAULT
31013108
{
31023109
$$ = lconsi(1, makeList1(NULL));
31033110
}
3104-
|TEMPLATE'='name
3111+
|TEMPLATEopt_equalname
31053112
{
31063113
$$ = lconsi(2, makeList1($3));
31073114
}
3108-
|TEMPLATE'='DEFAULT
3115+
|TEMPLATEopt_equalDEFAULT
31093116
{
31103117
$$ = lconsi(2, makeList1(NULL));
31113118
}
3112-
|ENCODING'='Sconst
3119+
|ENCODINGopt_equalSconst
31133120
{
31143121
intencoding;
31153122
#ifdef MULTIBYTE
@@ -3123,7 +3130,7 @@ createdb_opt_item: LOCATION '=' Sconst
31233130
#endif
31243131
$$ = lconsi(3, makeListi1(encoding));
31253132
}
3126-
|ENCODING'='Iconst
3133+
|ENCODINGopt_equalIconst
31273134
{
31283135
#ifdef MULTIBYTE
31293136
if (!pg_get_enconv_by_encoding($3))
@@ -3134,12 +3141,23 @@ createdb_opt_item: LOCATION '=' Sconst
31343141
#endif
31353142
$$ = lconsi(3, makeListi1($3));
31363143
}
3137-
|ENCODING'='DEFAULT
3144+
|ENCODINGopt_equalDEFAULT
31383145
{
31393146
$$ = lconsi(3, makeListi1(-1));
31403147
}
3148+
|OWNERopt_equalname
3149+
{
3150+
$$ = lconsi(4, makeList1($3));
3151+
}
3152+
|OWNERopt_equalDEFAULT
3153+
{
3154+
$$ = lconsi(4, makeList1(NULL));
3155+
}
31413156
;
31423157

3158+
opt_equal:'='{$$ =TRUE; }
3159+
|/*EMPTY*/{$$ =FALSE; }
3160+
;
31433161

31443162
/*****************************************************************************
31453163
*

‎src/backend/tcop/utility.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.125 2002/02/07 00:27:30 inoue Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.126 2002/02/24 20:20:20 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -624,8 +624,9 @@ ProcessUtility(Node *parsetree,
624624

625625
set_ps_display(commandTag="CREATE DATABASE");
626626

627-
createdb(stmt->dbname,stmt->dbpath,
628-
stmt->dbtemplate,stmt->encoding);
627+
createdb(stmt->dbname,stmt->dbowner,
628+
stmt->dbpath,stmt->dbtemplate,
629+
stmt->encoding);
629630
}
630631
break;
631632

‎src/include/commands/dbcommands.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: dbcommands.h,v 1.19 2001/11/05 17:46:33 momjian Exp $
10+
* $Id: dbcommands.h,v 1.20 2002/02/24 20:20:21 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#ifndefDBCOMMANDS_H
1515
#defineDBCOMMANDS_H
1616

17-
externvoidcreatedb(constchar*dbname,constchar*dbpath,
18-
constchar*dbtemplate,intencoding);
17+
externvoidcreatedb(constchar*dbname,constchar*dbowner,
18+
constchar*dbpath,constchar*dbtemplate,
19+
intencoding);
1920
externvoiddropdb(constchar*dbname);
2021

2122
#endif/* DBCOMMANDS_H */

‎src/include/nodes/parsenodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: parsenodes.h,v 1.152 2002/02/18 23:11:41 petere Exp $
10+
* $Id: parsenodes.h,v 1.153 2002/02/24 20:20:21 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -679,6 +679,7 @@ typedef struct CreatedbStmt
679679
{
680680
NodeTagtype;
681681
char*dbname;/* name of database to create */
682+
char*dbowner;/* name of owner (NULL = default) */
682683
char*dbpath;/* location of database (NULL = default) */
683684
char*dbtemplate;/* template to use (NULL = default) */
684685
intencoding;/* MULTIBYTE encoding (-1 = use default) */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp