ALTER_DATABASE(7pg17)
β¨ ALTER DATABASE π
okay bestie, we're in the ALTER DATABASE(7) page of the PostgreSQL 17.11 Documentation and honestly? this one's a glow-up command. it edits the attributes of a database β like giving your db a little makeover π
πΈ NAME πΈ
ALTER_DATABASE β change a database
β¨ 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 REFRESH COLLATION VERSION
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
π DESCRIPTION π
ALTER DATABASE changes the attributes of a database. that's the whole vibe. but there are so many forms and they each do their own little thing, so buckle up π¦
form one changes certain per-database settings (the details are down below π). only the database owner or a superuser can change these settings. periodt.
form two 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 heads up: the current database cannot be renamed. (connect to a different database if you need to do that, diva π
)
form three changes the owner of the database. to alter the owner, you must be able to SET ROLE to the new owning role, and you must have the CREATEDB privilege. (note that superusers have all these privileges automatically β main character energy, no cap π)
form four 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. this command physically moves any tables or indexes in the database's old default tablespace to the new tablespace π so like, be aware. the new default tablespace must be empty for this database, and no one can be connected to the database. tables and indexes in non-default tablespaces are unaffected β they're unbothered, they're thriving πΈ
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 β it ate and left no crumbs π₯ only the database owner or a superuser can change the session defaults for a database. 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.allowconnβ iffalsethen no one can connect to this database. like, nobody. not even your bestie πconnlimitβ how many concurrent connections can be made to this database.-1means no limit. go off, unlimited queen π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 πnew_nameβ the new name of the database. rebrand era β¨new_ownerβ the new owner of the database.new_tablespaceβ the new default tablespace of the database.
β οΈ this form of the command cannot be executed inside a transaction block. nope. not allowed. don't try it π
REFRESH COLLATION VERSIONβ update the database collation version. see Notes for background (scroll down bestie, it's short and sweet π)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 19 for more information about allowed parameter names and values. iykyk π
π¦ 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)). role-specific settings override database-specific ones if there is a conflict β role wins, always, sorry not sorry π
π EXAMPLES π
to disable index scans by default in the database test:
ALTER DATABASE test SET enable_indexscan TO off;
slay. iconic. exactly what she wanted π
β COMPATIBILITY β
the ALTER DATABASE statement is a PostgreSQL extension. not standard SQL, she's custom π
π«Ά SEE ALSO π«Ά
CREATE DATABASE(CREATE_DATABASE(7))DROP DATABASE(DROP_DATABASE(7))SET(7)CREATE TABLESPACE(CREATE_TABLESPACE(7))
and that's the whole page bestie!! π§β¨ this was PostgreSQL 17.11, dated 2026, and the page is ALTER DATABASE(7) β go forth and alter responsibly, mwah πππΈ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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