Updated on 2025-05-29 GMT+08:00

Managing Databases

Figure 1 Creating and Managing Databases

Prerequisites

You must have the permission to create a database or the SYSADMIN permission. For details about how to grant permissions, see Users and Permissions.

Context

  • GaussDB has four default template databases template0, template1, templatem, and templatea, and a default user database postgres. The default compatible database type of Postgres is A (that is, DBCOMPATIBILITY is set to A). In this compatible type, empty strings are processed as null values.
  • CREATE DATABASE creates a database by copying a template database By default, an A-compatible database copies templatea, and a non-A-compatible database copies template0. Do not use a client or other methods to connect to or operate the two template databases.
    • The template database does not contain any user table. You can view the attributes of the template database in the PG_DATABASE system catalog.
    • The template0 and templatea templates do not allow user connections. Only the initial user of the database and system administrators can connect to template1 and templatem.
    • When you create a database in A-compatible mode and specify template0 as the template, a notice is generated indicating that the templatea template is actually used.
    • templatem is a database name reserved by the system and cannot be a name of database created by users.
    • The name of templatea or templatem cannot be changed.
  • A database system consists of multiple databases. A client can connect to only one database at a time. Users cannot query data across databases. If GaussDB contains multiple databases, set the -d parameter to specify the database instance to be connected.

Precautions

Assume that the database encoding is SQL_ASCII. (You can run the show server_encoding; command to query the encoding used for storing data in the current database.) If the database object name contains multi-byte characters (such as Chinese) or if the object name length exceeds the allowed maximum (63 bytes), the database truncates the last byte (not the last character) of the object name. In this case, half characters may appear.

To resolve this problem, you need to:

  • Ensure that the name of the data object does not exceed the maximum length.
  • Change the default database storage code set (server_encoding) to UTF-8.
  • Exclude multi-byte characters from object names.
  • If you fail to delete an object by specifying its name after truncation, specify its original name to delete it, or manually delete it from the system catalogs on each node.

Procedure

  1. Create a database named db_tpcc.

    1
    2
    3
    4
    gaussdb=# CREATE DATABASE db_tpcc;
    CREATE DATABASE
    gaussdb=# CREATE DATABASE db_tpcc1 TEMPLATE templatea;
    CREATE DATABASE
    
    • Database names must comply with the general naming convention rules of SQL identifiers. The current role automatically becomes the owner of this new database.
    • If a database system is used to support independent users and projects, store them in different databases.
    • If the projects or users are associated with each other and share resources, store them in one database. However, you can divide them into different schemas. A schema is a logical structure, and the access permission for a schema is controlled by the permission system module.
    • A database name contains a maximum of 63 bytes and the excessive bytes at the end of the name will be truncated by the server. You are advised to specify a database name no longer than 63 bytes when you create a database.
    • New databases are created in the pg_default tablespace by default. Specify another tablespace.
      1
      2
      gaussdb=# CREATE DATABASE db_tpcc WITH TABLESPACE = hr_local;
      CREATE DATABASE
      

      hr_local indicates the tablespace name. For details about how to create a tablespace, see Managing Tablespaces.

    • After creating the db_tpcc database, you can perform other operations in the default postgres database. Alternatively, you can perform the following operations to exit the postgres database, connect to the db_tpcc database as a new user, and perform operations such as creating tables:
      gaussdb=# \q
      gsql -d db_tpcc -p 8000 -U joe
      Password for user joe:
      gsql ((GaussDB Kernel XXX.X.XXX build f521c606) compiled at 2021-09-16 14:55:22 commit 2935 last mr 6385 release)
      Non-SSL connection (SSL connection is recommended when requiring high-security)
      Type "help" for help.

  2. View databases.

    • Run the \l meta-command to view the database list of the database system.
      1
      gaussdb=# \l
      
    • Query the database list in the pg_database system catalog.
      1
      gaussdb=# SELECT datname FROM pg_database;
      

  3. Modify the database.

    You can modify database configuration such as the database owner, name, and default settings.

    • Set the default search path for the database.
      1
      2
      gaussdb=# ALTER DATABASE db_tpcc SET search_path TO pa_catalog,public;
      ALTER DATABASE
      
    • Rename the database.
      1
      2
      gaussdb=# ALTER DATABASE db_tpcc RENAME TO human_tpcds;
      ALTER DATABASE
      

  4. Delete the database.

    You can run the command in DROP DATABASE to drop a database. This command removes the database system catalog and permanently deletes the data files on the disk. Only the database owner or users with the SYSADMIN permission can perform the drop operation. If there are active database connections, the drop operation will fail. Ensure that you have connected to other databases before performing the drop operation.

    Delete the database.
    1
    2
    gaussdb=# DROP DATABASE human_tpcds;
    DROP DATABASE