ALTER_DATABASE(7pg18)
โจ ALTER DATABASE โจ
okay bestie, welcome to the ALTER DATABASE page from the PostgreSQL 18.6 Documentation ๐ this one's a manual page (that's the (7) vibe) and it's literally all about changing a database. yes. the whole thing. let's get into it ๐ซถ
๐ธ 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 ๐ฆ
so ALTER DATABASE changes the attributes of a database. that's the whole gig. main character behaviour fr ๐
the first form changes certain per-database settings (details are down below, keep scrolling ๐). only the database owner or a superuser can change these settings. no randos.
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, the current database cannot be renamed โ not me trying to rename the db i'm literally connected to ๐ญ (connect to a different database if you need to do that, bestie).
the third form 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. (heads up: superusers have all these privileges automatically, they're built different ๐)
the fourth form changes the default tablespace of the database. only the database owner or a superuser can do this, and 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 โ like actually moving files, no cap. 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 just chilling. the method used to copy files to the new tablespace is affected by the file_copy_method setting.
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. certain variables cannot be set this way, or can only be set by a superuser. it's giving exclusive ๐
๐ PARAMETERS ๐
nameโ the name of the database whose attributes are to be altered. that's the one we're messing with ๐allowconnโ iffalsethen no one can connect to this database. the door? locked. ๐ช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. choose your fighter ๐ฅnew_nameโ the new name of the database. glow-up name change ๐new_ownerโ the new owner of the database. handing over the keys ๐new_tablespaceโ the new default tablespace of the database.
this form of the command cannot be executed inside a transaction block. nope. not happening. don't even try it ๐ซ
REFRESH COLLATION VERSIONโ update the database collation version. see Notes for background, i'm not gonna spoil it ๐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. do your reading bestie, it's giving homework but in a cute way ๐
๐ 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 said "i'm the main character here" ๐
๐ EXAMPLES ๐
to disable index scans by default in the database test:
ALTER DATABASE test SET enable_indexscan TO off;
periodt. that's the example ๐
โญ COMPATIBILITY โญ
the ALTER DATABASE statement is a PostgreSQL extension. it's not standard SQL, it's our thing. iykyk ๐ซถ
๐ 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 18.6, dated 2026, and it absolutely ate ๐ฅ go alter a database (responsibly, and not the one you're connected to ๐ญ). mwah ๐๐โจ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms