Yogesh Chauhan's Blog

ALTER DATABASE in PostgreSQL

in Postgres on June 25, 2020

ALTER DATABASE in PostgreSQL

ALTER DATABASE statement change the database in various ways. 

ALTER DATABASE changes the attributes of a database.

Syntax


ALTER DATABASE name [ [ WITH ] option [ ... ] ]

where option can be:

    CONNECTION LIMIT connlimit

The above query changes certain per-database settings. Only the database owner or a superuser can change these settings.

Change the name of the database


ALTER DATABASE name 
RENAME TO new_name

The above query 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.

The current database cannot be renamed. Connect to a different database if you need to do that.

Change the name of the database owner


ALTER DATABASE name 
OWNER TO new_owner

The above query 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.

Superusers have all these privileges automatically.

Change tablespace


ALTER DATABASE name 
SET TABLESPACE new_tablespace

The above query 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 tables and indexes in non-default tablespaces are not affected.

Change the session default for a run-time configuration


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

The queries above 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.

The parameters and what do they stand for

name: The name of the database whose attributes are to be altered.

connlimit: How many concurrent connections can be made to this database. -1 means no limit.

new_name: The new name of the database.

new_owner: The new owner of the database.

new_tablespace: The new default tablespace of the database.

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.

Examples

To disable index scans by default in the database test:


ALTER DATABASE test 
SET enable_indexscan TO off;

Credit: Postgresql Docs


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
The ALPHA function in Envision BasicEnvision BasicLearn to Implement Estimated Reading Time using PHP Part 2: Final Implementation with Source CodePHPHow to add a ribbon inside a container using CSS?CSSIs there a way to do a FULL OUTER JOIN in MySQL?SQL/MySQLHow to add before after image effect using pure CSS?CSSHow to add new elements with swing animation using JavaScript and CSS?CSS