Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

ALTER DATABASE

Updated on 2024-08-20 GMT+08:00

Description

Modifies a database, including its name, owner, object isolation, and connection limitation.

Precautions

  • Only the database owner or a user granted with the ALTER permission can run the ALTER DATABASE command. By default, system administrators have the permission. The following are permission constraints depending on the attributes to be modified:
    • To modify the database name, you must have the CREATEDB permission.
    • To modify a database owner, you must be a database owner or system administrator and a member of the new owner role, with the CREATEDB permission.
    • To modify the default tablespace of the database, the user must have the CREATE permission on the tablespace. This statement physically migrates tables and indexes in a default tablespace to a new tablespace. Note that tables and indexes outside the default tablespace are not affected.
  • You are not allowed to rename a database in use. To rename it, connect to another database.

Syntax

  • Modify the maximum number of connections to the database.
    ALTER DATABASE database_name 
        [ WITH ] CONNECTION LIMIT connlimit;
  • Rename the database.
    ALTER DATABASE database_name 
        RENAME TO new_name;
  • Change the database owner.
    ALTER DATABASE database_name 
        OWNER TO new_owner;
  • Change the default tablespace of the database.
    ALTER DATABASE database_name 
        SET TABLESPACE new_tablespace;
    NOTE:

    If some tables or objects in the database have been created in new_tablespace, the default tablespace of the database cannot be changed to new_tablespace. An error will be reported during the execution.

  • Modify the session parameter value of the database.
    ALTER DATABASE database_name 
        SET configuration_parameter { { TO | = } { value | DEFAULT } | FROM CURRENT };
  • Reset the database configuration parameter.
    ALTER DATABASE database_name RESET 
        { configuration_parameter | ALL };
  • Modify the object isolation attribute of the database.
    ALTER DATABASE database_name [ WITH ] { ENABLE | DISABLE } PRIVATE OBJECT;
    NOTE:
    • To modify the object isolation attribute of a database, the database must be connected. Otherwise, the modification will fail.
    • For a new database, the object isolation attribute is disabled by default. After the database object isolation attribute is enabled, the database automatically adds row-level security policies to the system catalogs PG_CLASS, PG_ATTRIBUTE, PG_PROC, PG_NAMESPACE, PGXC_SLICE, and PG_PARTITION. Common users can only view the objects (tables, functions, views, and columns) that they have the permission to access. This attribute does not take effect for administrators. After this attribute is enabled, administrators can still view all database objects.
  • Change the database time zone.
    1
    ALTER DATABASE database_name SET DBTIMEZONE = time_zone;
    
  • Migrate a specified bucket from one DN to another. The current version does not support this function.
    1
    2
    ALTER DATABASE database_name
        MOVE BUCKETS(bucketlist) FROM datanode_name TO datanode_name;
    
  • Enable or disable the ILM feature of the database.
    1
    ALTER DATABASE set ilm = { on | off } ;
    

Parameters

  • database_name

    Specifies the name of the database whose attributes are to be modified.

    Value range: a string. It must comply with the naming convention.

  • connlimit

    Specifies the maximum number of concurrent connections that can be made to this database (excluding administrators' connections).

    Value range: an integer ranging from –1 to 231 – 1. You are advised to set this parameter to an integer ranging from 1 to 50. The default value –1 indicates that there is no restriction on the number of concurrent connections.

  • new_name

    Specifies the new name of a database.

    Value range: a string. It must comply with the naming convention.

  • new_owner

    Specifies the new owner of a database.

    Value range: a string. It must be a valid username.

  • new_tablespace

    Specifies the new default tablespace of a database. The tablespace exists in the database. The default tablespace is pg_default.

    Value range: a string. It must be a valid tablespace name.

  • configuration_parameter
    • value

      Sets a specified database session parameter to a specified value. If the value is DEFAULT or RESET, the default setting is used in the new session. OFF disables the setting.

      Value range: a string.

      • DEFAULT
      • OFF
      • RESET
      • User-specified value: The value must meet the restriction of the modified parameter.
    • FROM CURRENT

      Uses the value of configuration_parameter of the current session.

  • time_zone

    Sets the time zone of the database specified by database_name. You must have the permission on the corresponding database.

    Value range: a string.

    • Time zones supported by the system and their abbreviations.
    • -15:59~+15:00
  • RESET configuration_parameter

    Resets the specified database session parameter.

  • RESET ALL

    Resets all database session parameters.

NOTE:
  • If you modify the default tablespace of a database, the tables and indexes in the old tablespace are moved into the new tablespace. This operation does not affect the tables or indexes in other non-default tablespaces.
  • The modified database session parameter values will take effect in the next session.

Examples

  • Rename the database.
    -- Create the database testdb.
    gaussdb=# CREATE DATABASE testdb;
    
    -- Rename testdb to test_db1.
    gaussdb=# ALTER DATABASE testdb RENAME TO test_db1;
  • Change the maximum number of database connections.
    -- Change the maximum number of connections to test_db1 to 100.
    gaussdb=# ALTER DATABASE test_db1 WITH CONNECTION LIMIT 100;
    
    -- View information about test_db1.
    gaussdb=# SELECT datname,datconnlimit FROM pg_database WHERE datname = 'test_db1';
     datname  | datconnlimit 
    ----------+--------------
     test_db1 |          100
    (1 row)
  • Change the database owner.
    -- Create user scott.
    gaussdb=# CREATE USER scott PASSWORD '********';
    
    -- Change the owner of test_db1 to scott.
    gaussdb=# ALTER DATABASE test_db1 OWNER TO scott;
    
    -- View information about test_db1.
    gaussdb=# SELECT t1.datname, t2.usename 
              FROM pg_database t1, pg_user t2 
              WHERE t1.datname='test_db1' AND t1.datdba=t2.usesysid;
     datname  | usename 
    ----------+---------
     test_db1 | scott
    (1 row)
  • Change the default tablespace of the database.
    -- Create a tablespace.
    gaussdb=# CREATE TABLESPACE tbs_data1 RELATIVE LOCATION 'tablespace1/tbs_data1';
    
    -- Modify the default tablespace of test_db1.
    gaussdb=# ALTER DATABASE test_db1 SET TABLESPACE tbs_data1;
    
    -- View information about test_db1.
    gaussdb=# SELECT t1.datname AS database, t2.spcname AS tablespace 
              FROM pg_database t1, pg_tablespace t2 
              WHERE t1.dattablespace = t2.oid AND 
                    t1.datname = 'test_db1';
     database | tablespace 
    ----------+------------
     test_db1 | tbs_data1
    (1 row)
  • Modify the object isolation attribute of the database.
    -- Create user jack.
    gaussdb=# CREATE USER jack PASSWORD '********';
    
    -- Create the test_tbl1 table in test_db1.
    gaussdb=# \c test_db1
    test_db1=# CREATE TABLE test_tbl1(c1 int,c2 int);
    
    -- Switch to user jack and view pg_tables.
    test_db1=# SET ROLE jack PASSWORD '********';
    test_db1=> SELECT tablename FROM pg_tables WHERE tablename = 'test_tbl1';
     tablename 
    -----------
     test_tbl1
    (1 row)
    
    -- Modify the object isolation attribute.
    test_db1=> SET ROLE scott PASSWORD '********';              
    test_db1=> ALTER DATABASE test_db1 ENABLE PRIVATE OBJECT;
    
    -- Switch to user jack and view pg_tables.
    test_db1=> SET ROLE jack PASSWORD '********';
    
    -- Due to the isolation attribute, 0 data records can be queried.
    test_db1=> SELECT tablename FROM pg_tables WHERE tablename = 'test_tbl1';
     tablename 
    -----------
    (0 rows)
    
    -- Switch to the default user and perform the deletion.
    test_db1=> RESET ROLE;
    test_db1=# DROP TABLE public.test_tbl1;
    
    -- Switch to the default database. Change the database name based on actual situation.
    test_db1=# \c postgres
    gaussdb=# DROP DATABASE test_db1;
    gaussdb=# DROP TABLESPACE tbs_data1;
    gaussdb=# DROP USER jack;
    gaussdb=# DROP USER scott;

Helpful Links

CREATE DATABASE and DROP DATABASE

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback