ALTER_DATABASE(7)

โœจ 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 โ€” if false then no one can connect to this database. the door? locked. ๐Ÿšช
  • connlimit โ€” how many concurrent connections can be made to this database. -1 means no limit. unlimited connections, unlimited chaos โœจ
  • istemplate โ€” if true, then this database can be cloned by any user with CREATEDB privileges; if false, 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. if value is DEFAULT or, equivalently, RESET is used, the database-specific setting is removed, so the system-wide default setting will be inherited in new sessions. use RESET ALL to clear all database-specific settings. SET FROM CURRENT saves 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 ๐Ÿ’–


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 ๐Ÿ’‹๐Ÿ’–โœจ