Thread: BUG #17534: 'tablespace' option crushes 'create database' query with 'permission denied' message

The following bug has been logged on the website:

Bug reference:      17534
Logged by:          Nick Nalbantov
Email address:      akdmnnakdm@gmail.com
PostgreSQL version: 14.4
Operating system:   Windows
Description:

Here's an old problem (look at

https://dba.stackexchange.com/questions/204807/why-do-i-get-a-postgresql-permission-error-when-specifying-a-tablespace-in-the?rq=1),
but it's still reproducible in PG 15b1 on Windows and PG13 on Debian 10

If some user with createdb privilege would try to create database with
'tablespace = pg_default' option, query will fail with ERROR:  42501
But same query without  'tablespace = pg_default' option would work fine,
and new database will be created at pg_default tablespace as expected.

Steps to reproduce:

psql -U postgres -d postgres
create role test with login password '123' createdb;
\q

psql -U test -d postgres
\set VERBOSITY verbose
CREATE DATABASE test 
OWNER = test 
ENCODING = UTF8 
TABLESPACE = pg_default; -- query works as expected if this line is
missing

ERROR:  42501: permission denied for tablespace pg_default
LOCATION:  aclcheck_error, aclchk.c:3652


PG Bug reporting form <noreply@postgresql.org> writes:
> If some user with createdb privilege would try to create database with
> 'tablespace = pg_default' option, query will fail with ERROR:  42501
> But same query without  'tablespace = pg_default' option would work fine,
> and new database will be created at pg_default tablespace as expected.

I do not think this is a bug.  The default tablespace is determined
by (copied from) the template database you use, and a user without
any tablespace-related privileges shouldn't be able to override that.

            regards, tom lane