ALTER_DATABASE(7pg13)

πŸ“¦ version 13.23, in opensuse-leap 16.0

πŸ’… ALTER DATABASE β€” change a database

okay bestie, welcome to ALTER DATABASE(7) from the PostgreSQL 13.23 Documentation ✨ this is the page where we learn how to glow-up a database's whole identity β€” its name, its owner, its tablespace, its settings β€” all the good stuff πŸŽ€


🌸 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_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

no cap, that's a lot of forms β€” but each one is a different little makeover, and we're gonna go through every single one πŸ’–


πŸ¦‹ DESCRIPTION πŸ¦‹

So like, ALTER DATABASE changes the attributes of a database. That's the whole vibe. ✨

Form one β€” the per-database settings. The first form changes certain per-database settings (details are down in PARAMETERS, keep scrolling bestie). Only the database owner or a superuser can change these settings. πŸ‘‘

Form two β€” the rename. The second form changes the name of the database. Only the database owner or a superuser can rename a database; non-superuser owners must also have the CREATEDB privilege. And heads up β€” the current database cannot be renamed πŸ™ˆ (connect to a different database if you need to do that, iykyk).

Form three β€” the owner swap. 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 β€” must be nice 😭.)

Form four β€” the tablespace glow-up. 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. 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're literally fine, leave them alone πŸ’…

The remaining forms β€” session defaults. 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 β€” yes, it really said that, it wins πŸ”₯ 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. the main character, obviously πŸ‘‘
  • allowconn β€” if false then no one can connect to this database. like, at all. doors locked, vibe sealed πŸ’€
  • connlimit β€” how many concurrent connections can be made to this database. -1 means no limit. the more the merrier fr fr πŸ“
  • 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. 🧚
  • new_name β€” the new name of the database. a rebrand!! ✨
  • 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. no exceptions bestie.
  • 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. πŸ“š


πŸ’œ 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 the vibe check, sorry not sorry 😌


πŸŽ€ EXAMPLES πŸŽ€

To disable index scans by default in the database test:

ALTER DATABASE test SET enable_indexscan TO off;

slay, that's it, that's the example πŸ’…


⭐ COMPATIBILITY ⭐

The ALTER DATABASE statement is a PostgreSQL extension. we made it ourselves, iconic behavior honestly 🫢


🌸 SEE ALSO 🌸


and that's the whole page bestie!! πŸ¦‹πŸ’– brought to you by PostgreSQL 13.23, dated 2025, and this little ALTER DATABASE(7) manual moment πŸŽ€ go forth and alter responsibly, mwah βœ¨πŸ‘‘

πŸ“¦ where this page comes from