Creating a Database

After the database is installed, a database named postgres is generated by default. You need to create your own database.

Syntax

  • Create a database.

    CREATE DATABASE database_name;
    
  • View the database.

    • Run \l to view the existing database.

      \l
      
    • Run \c + Database name to access the existing database.

      \c dbname
      
  • Modify the database.

    ALTER DATABASE database_name RENAME TO new_name;
    
  • Delete the database.

    DROP DATABASE database_name ;
    

Parameter Description

  • database_name

    Specifies the name of the database to be created, modified, or deleted.

  • new_name

    Specifies the new name of a database.

Examples

  • Create the db_tpcc database.

    openGauss=# CREATE DATABASE db_tpcc;
    CREATE DATABASE
    
  • Run \l to view the existing database.

    openGauss=# \l
                            List of databases
       Name    | Owner | Encoding  | Collate | Ctype | Access privileges
    -----------+-------+-----------+---------+-------+-------------------
     db_tpcc   | omm   | SQL_ASCII | C       | C     |
     mydb      | omm   | GBK       | C       | C     |
     postgres  | omm   | SQL_ASCII | C       | C     |
     template0 | omm   | SQL_ASCII | C       | C     | =c/omm           +
               |       |           |         |       | omm=CTc/omm
     template1 | omm   | SQL_ASCII | C       | C     | =c/omm           +
               |       |           |         |       | omm=CTc/omm
    (5 rows)
    
  • Create a database (this does not mean that the database is used). You need to specify that the created database is used. Run \c + Database name to access the db_tpcc database.

    openGauss=# \c db_tpcc
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "db_tpcc" as user "omm".
    db_tpcc=#
    
  • Switch to the postgres database.

    db_tpcc=# \c postgres
    
  • Change the db_tpcc database name to tpcc.

    openGauss=# ALTER DATABASE db_tpcc RENAME TO tpcc;
    ALTER DATABASE
    
  • Delete the tpcc database.

    openGauss=# DROP DATABASE tpcc;
    DROP DATABASE
    
Feedback
编组 3备份
    openGauss 2024-04-22 00:47:24
    cancel