ALTER_DATABASE(7pg14)
π ALTER DATABASE π
okay so bestie, welcome to the ALTER DATABASE page from the PostgreSQL 14.24 Documentation β¨ this is the one where you go mess with a whole entire database's vibe β its name, its owner, its tablespace, its settings β all from one command. it's giving admin main character energy π
β¨ NAME β¨
ALTER_DATABASE β change a database π¦
like, literally change a database. that's the whole job.
π SYNOPSIS π
ALTER DATABASE name [ [ WITH ] option [ ... ] ]
where option can be:
ALLOW_CONNECTIONS allowconn
CONNECTION LIMIT connlimit
IS_TEMPLATE istemplate
ALTER DATABASE name RENAME TO new_name
ALTER DATABASE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER DATABASE name SET TABLESPACE new_tablespace
ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL
five whole forms, each one a different little slay π pick the one you need, okay?
π DESCRIPTION π
ALTER DATABASE changes the attributes of a database. periodt. β¨
the first form changes certain per-database settings (details down below in PARAMETERS, keep scrolling bestie). only the database owner or a superuser can change these settings. no random people touching your settings π
the second form changes the name of the database. only the database owner or a superuser can rename a database β and non-superuser owners must also have the CREATEDB privilege. also?? you cannot rename the current database π (connect to a different database if you need to do that, iykyk.)
the third form changes the owner of the database. to alter the owner you must own the database and also be a direct or indirect member of the new owning role, and you must have the CREATEDB privilege. (note that superusers have all these privileges automatically β they're just built different π)
the fourth form changes the default tablespace of the database. only the database owner or a superuser can do this; you must also have create privilege for the new tablespace. heads up: this command physically moves any tables or indexes in the database's old default tablespace to the new tablespace π the new default tablespace must be empty for this database, and no one can be connected to the database while this happens. tables and indexes in non-default tablespaces are unaffected β they stay right where they are, unbothered.
the remaining forms change the session default for a run-time configuration variable for a PostgreSQL database. whenever a new session is subsequently started in that database, the specified value becomes the session default value π± the database-specific default overrides whatever setting is present in postgresql.conf or has been received from the postgres command line. only the database owner or a superuser can change the session defaults for a database. and plot twist: certain variables cannot be set this way, or can only be set by a superuser π
π¦ PARAMETERS π¦
nameβ the name of the database whose attributes are to be altered. the star of the show π«allowconnβ iffalsethen no one can connect to this database. nobody. not even your bestie. πͺπconnlimitβ how many concurrent connections can be made to this database.-1means no limit. unlimited connections, unlimited chaos π₯istemplateβ iftrue, then this database can be cloned by any user withCREATEDBprivileges; iffalse, then only superusers or the owner of the database can clone it. gatekeeping, but make it permissions πnew_nameβ the new name of the database. glow-up name change β¨new_ownerβ the new owner of the database. handing over the crown πnew_tablespaceβ the new default tablespace of the database.
β οΈ this form of the command cannot be executed inside a transaction block. just. don't. it's not that kind of command, bestie π
configuration_parameter,valueβ set this database's session default for the specified configuration parameter to the given value. ifvalueisDEFAULTor, equivalently,RESETis used, the database-specific setting is removed, so the system-wide default setting will be inherited in new sessions 𧬠useRESET ALLto clear all database-specific settings.SET FROM CURRENTsaves the session's current value of the parameter as the database-specific value.
see SET(7) and Chapter 20 for more information about allowed parameter names and values π
πΈ NOTES πΈ
it is also possible to tie a session default to a specific role rather than to a database; see ALTER ROLE (ALTER_ROLE(7)). and listen β role-specific settings override database-specific ones if there is a conflict. role wins. that's the hierarchy, periodt π
π EXAMPLES π
to disable index scans by default in the database test:
ALTER DATABASE test SET enable_indexscan TO off;
look at that. one line. iconic. no cap β¨
β COMPATIBILITY β
the ALTER DATABASE statement is a PostgreSQL extension. it's not standard SQL, it's our thing π«Ά
π SEE ALSO π
CREATE DATABASE (CREATE_DATABASE(7)), DROP DATABASE (DROP_DATABASE(7)), SET(7), CREATE TABLESPACE (CREATE_TABLESPACE(7)) β go read those too, they're all linked in the group chat π§
and that's the whole page, bestie!! π brought to you by PostgreSQL 14.24 and dated 2026 β mwah π go alter some databases responsibly ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms