From cfbfdc557d166ec559668d18d9769544f3c4fbbc Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 26 May 2004 13:57:04 +0000 Subject: This patch implement the TODO [ALTER DATABASE foo OWNER TO bar]. It was necessary to touch in grammar and create a new node to make home to the new syntax. The command is also supported in E CPG. Doc updates are attached too. Only superusers can change the owner of the database. New owners don't need any aditional privileges. Euler Taveira de Oliveira --- src/backend/commands/dbcommands.c | 48 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/backend/commands/dbcommands.c') diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 9114983e75d..02c1bf8e204 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.133 2004/05/26 04:41:10 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.134 2004/05/26 13:56:45 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -776,6 +776,52 @@ AlterDatabaseSet(AlterDatabaseSetStmt *stmt) } +/* + * ALTER DATABASE name OWNER TO newowner + */ +void +AlterDatabaseOwner(const char *dbname, const char *newowner) +{ + AclId newdatdba; + HeapTuple tuple, + newtuple; + Relation rel; + ScanKeyData scankey; + SysScanDesc scan; + + rel = heap_openr(DatabaseRelationName, RowExclusiveLock); + ScanKeyInit(&scankey, + Anum_pg_database_datname, + BTEqualStrategyNumber, F_NAMEEQ, + NameGetDatum(dbname)); + scan = systable_beginscan(rel, DatabaseNameIndex, true, + SnapshotNow, 1, &scankey); + tuple = systable_getnext(scan); + if (!HeapTupleIsValid(tuple)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_DATABASE), + errmsg("database \"%s\" does not exist", dbname))); + + /* obtain sysid of proposed owner */ + newdatdba = get_usesysid(newowner); /* will ereport if no such user */ + + /* changing owner's database for someone else: must be superuser */ + /* note that the someone else need not have any permissions */ + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("must be superuser to change owner's database for another user"))); + + /* change owner */ + newtuple = heap_copytuple(tuple); + ((Form_pg_database) GETSTRUCT(newtuple))->datdba = newdatdba; + simple_heap_update(rel, &tuple->t_self, newtuple); + CatalogUpdateIndexes(rel, newtuple); + + systable_endscan(scan); + heap_close(rel, NoLock); +} + /* * Helper functions -- cgit v1.2.3