ALTER_DATABASE(7pg17)

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

✨ 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 β€” if false then no one can connect to this database. like, nobody. not even your bestie πŸ™ˆ
  • connlimit β€” how many concurrent connections can be made to this database. -1 means no limit. go off, unlimited queen πŸ’€
  • 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. 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. 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. 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 🫢


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