EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL ALTER DATABASE

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL ALTER DATABASE

PostgreSQL ALTER DATABASE

Introduction to PostgreSQL ALTER DATABASE

PostgreSQL alter database statement is used to alter the database features like changing the ownership, change the name of the database, set the new tablespace of the database, and set the configuration parameter for the database. Alter database command is essential and useful in PostgreSQL to change the feature of the database. To change the database settings, we need to privilege of database owner or admin user of the database.  If we want to change the name of the database, we have used alter database command in PostgreSQL; also, if we want to change the database’s ownership, we have used the same command.

Syntax

Below is the syntax of the alter database statement in PostgreSQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

1. Change the name

Alter database name_of_database rename to new_database_name;

2. Change ownership

Alter database name_of_database owner to new_owner_of_the_database;

3. Change the tablespace

Alter database name_of_database set tablespace new_tablespace_name;

4. Change the defaults runtime parameter

Alter database name_of_database set configuration_parameter = value (New value of database which we have setting.)

Parameters

Below is the parameter description syntax of alter database statement in PostgreSQL.

  • Alter database: Alter database statement is used to change the database features in PostgreSQL.
  • Database name: This is defined as the name of the database from which we have changed the database’s features.
  • Owner: This keyword is defined as changing the ownership of the database. We have to change the ownership of the database by using alter database statements. We have to change the database owner after the database creating.
  • Rename: This keyword is defined as changing the name of the database. We have to change the name of the database by using the alter database statement. We have to change the database name after database creation.
  • Tablespace: This keyword is defined as changing the tablespace of the database. We have to change the tablespace of the database by using the alter database statement. We have to change the database tablespace after database creation.
  • Configuration parameter: This is defined as changing the configuration parameter of the database. We have to change the database configuration parameter by using the alter database statement.
  • New database name: This is defined as a new database name which was we have defined to the database.
  • New tablespace name: This is defined as a new tablespace name which was we have defined to the database.
  • New owner: This is defined as the new owner name, which was we have defined in the database.

How to ALTER DATABASE statement work in PostgreSQL?

Below is the working of the alter database statement in PostgreSQL.

  • If we have to use alter database statements in PostgreSQL, we need to have privileges of the owner of the database or need to have admin privileges.
  • The below example shows we need to have privileges of admin or owner of the database to use the alter database statement in PostgreSQL.

psql -U db_test -d db_testing
alter database db_testing rename to db_testing_new;
psql -U postgres
alter database db_testing rename to db_testing_new;
\l+

Output:

PostgreSQL ALTER DATABASE - 1

  • The above first example shows that we have to alter the database using the username as db_test, but it will not work; it shows the error as “ERROR: must be the owner of database db_testing”.
  • In the second example, we have altered the database using the username as Postgres; after using this user, we have changed the database’s name to db_testing_new.
  • We can also set the concurrent connection limit to the database by using the alter database statement in PostgreSQL.
  • We can set the session default setting by using the given configuration parameter. If the value is the default, then database-specific settings were removed. Then system-wide settings are inherited to the new sessions.
  • We can also disable an index scan of the database by using the alter database statement in PostgreSQL.
  • Alter database is PostgreSQL extension used in PostgreSQL. We can change the attribute of the database by using an alter statement.

Examples to Implement PostgreSQL ALTER DATABASE

Below are the examples mentioned:

1. Change the Name

Below example shows that alter the database to change the database name. We are changing the database name of db_testing_new to db_testing using alter database statement. We have used Postgres user to change the name of the database in PostgreSQL.

Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (2,948 ratings)
Course Price

View Course

Related Courses

PostgreSQL ALTER DATABASE - 2

2. Change the Ownership

Below example shows that alter the database to change the database ownership. We are changing the db_testing database ownership from Postgres user to db_test user using alter database statement. We have used Postgres user to change the ownership of the db_testing database in PostgreSQL.

Code:

\l+
alter database db_testing owner to db_test;
\l+

Output:

Change the ownership

3. Change the Tablespace

Below example shows that alter the database to change the database tablespace. We are changing the db_testing database tablespace from default tablespace to my_test tablespace using the alter database statement. We have used Postgres users to change the tablespace of the db_testing database in PostgreSQL.

Code:

create tablespace my_test location '/new_test';
\db
ALTER DATABASE db_testing SET TABLESPACE my_test;
\l+

Output:

Change the tablespace

4. Configuration Parameter

Below example shows that alter the database to change the database configuration parameter. We have to change the database configuration parameter to disable the index scan of db_testing using alter database statement. We have used Postgres users to change the configuration parameter of the db_testing database in PostgreSQL.

Configuration parameter

Recommended Articles

This is a guide to PostgreSQL ALTER DATABASE. Here we discuss an introduction, syntax, parameters with how it works, and examples to implement. You can also go through our other related articles to learn more –

  1. PostgreSQL Database
  2. PostgreSQL Date Functions
  3. PostgreSQL Data Types
  4. PostgreSQL CASE Statement

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More