Updated on 2023-10-23 GMT+08:00

Before You Start

This section explains how to use databases, including creating databases and tables, inserting data to tables, and querying data in tables.

Prerequisites

GaussDB is running properly.

Procedure

  1. Connect to a database. For details, see Connecting to a Database.
  2. Create a database user.

    Only administrators that are created during database installation can access the initial database by default. You can also create other database users.

    1
    openGauss=# CREATE USER joe WITH PASSWORD "xxxxxxxxx";
    

    If the following information is displayed, the table has been created:

    1
    CREATE ROLE
    

    In this case, you have created a user named joe, and the user password is xxxxxxxxx.

    Run the following command to set user joe as the system administrator.

    openGauss=# GRANT ALL PRIVILEGES TO joe;

    Run the GRANT command to set related permissions. For details, see GRANT.

    Note: For details about how to create users, see Managing Users and Their Permissions.

  3. Create a database.

    1
    openGauss=# CREATE DATABASE db_tpcc OWNER joe;
    

    If the following information is displayed, the database has been created:

    1
    CREATE DATABASE
    

    After db_tpcc database is created, you can run the following command to exit the postgres database and log in to the db_tpcc database as the user you created for more operations. You can also continue using the default postgres database.

    openGauss=# \q
    gsql -d db_tpcc -p 8000 -U joe
    Password for user joe:
    gsql((GaussDB Kernel VxxxRxxxCxx 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.
     
    db_tpcc=> 

    Create a schema.

    db_tpcc=> CREATE SCHEMA joe AUTHORIZATION joe;

    If the following information is displayed, the schema has been created:

    1
    CREATE SCHEMA
    

    Note:

    New databases are created in the pg_default tablespace by default. To specify another tablespace, run the following statement:

    1
    2
    openGauss=# 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 Creating and Managing Tablespaces.

  4. Create a table.

    • Create a table named mytable that has only one column. The column name is firstcol and the column type is integer.
      1
      db_tpcc=>  CREATE TABLE mytable (firstcol int);
      
      1
      CREATE TABLE
      
    • Insert data into the table.
      1
      db_tpcc=> INSERT INTO mytable values (100);
      

      If the following information is displayed, the data has been inserted:

      1
      INSERT 0 1
      
    • View the data in the table.
      1
      2
      3
      4
      5
      db_tpcc=> SELECT * from mytable;
       firstcol 
      ----------
            100
      (1 row)
      

    Note:

    • By default, new database objects, such as the newly created table, are created in the $user schema. For more details about schemas, see Creating and Managing Schemas.
    • For more details about how to create a table, see Creating and Managing Tables.
    • In addition to the created tables, a database contains many system catalogs. These system catalogs contain database installation information and information about various queries and processes in GaussDB. You can collect information about the database by querying system catalogs. For details, see Querying a System Catalog.

      GaussDB supports hybrid row-column store, providing high query performance for interaction analysis in complex scenarios. For details about how to select a store method, see Planning a Storage Model.