openGauss学习笔记-09 openGauss 简单数据管理-创建数据库

数据库安装完成后,默认生成名称为postgres的数据库。您需要自己创建一个新的数据库。

9.1 语法格式

  • 创建数据库

    CREATE DATABASE database_name;
    
  • 查看数据库

    • 使用“\l”用于查看已经存在的数据库。

      \l
      
    • 使用 “\c + 数据库名” 进入已存在数据库。

      \c dbname
      
  • 修改数据库

    ALTER DATABASE database_name RENAME TO new_name;
    
  • 删除数据库

    DROP DATABASE database_name;
    

9.2 参数说明

  • database_name

    要创建、修改或者删除的数据库名称。

  • new_name

    数据库的新名称。

9.3 示例

  • 创建一个新的数据库db_superman。

    openGauss=# CREATE DATABASE db_superman;
    

    当结果显示如下信息,则表示创建成功:

    CREATE DATABASE
    
  • 使用“\l”用于查看已经存在的数据库。

    openGauss=# \l
                                   List of databases
        Name     | Owner | Encoding |   Collate   |    Ctype    | Access privileges 
    -------------+-------+----------+-------------+-------------+-------------------
     db_superman | omm   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     finance     | omm   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     postgres    | omm   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     school      | omm   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0   | omm   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/omm           +
                 |       |          |             |             | omm=CTc/omm
     template1   | omm   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/omm           +
                 |       |          |             |             | omm=CTc/omm
    (6 rows)
    
    openGauss=# 
    
  • 创建数据库并不会选择使用它。您需要明确的指定使用新创建的数据库。使用 “\c + 数据库名” 来进入db_superman数据库。

    openGauss=# \c db_superman
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "db_superman" as user "omm".
    db_superman=# 
    
  • 切换数据库为postgres数据库。

    db_superman=# \c postgres
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "postgres" as user "omm".
    openGauss=# 
    
  • 将db_superman数据库名称修改为superman。

    openGauss=# ALTER DATABASE db_superman RENAME TO superman;
    ALTER DATABASE
    openGauss=#
    

    当结果显示如下信息,则表示修改成功:

    ALTER DATABASE
    
  • 删除数据库superman。

    openGauss=# DROP DATABASE superman;
    DROP DATABASE
    openGauss=# 
    

    当结果显示如下信息,则表示删除成功:

    DROP DATABASE
    

openGauss学习笔记-09 openGauss 简单数据管理-创建数据库-LMLPHP

07-15 08:55