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 runs 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 the cluster 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 user has been created:

    1
    CREATE ROLE
    

    In this case, you have created a user named joe, and the user password is xxxxxxxxxx. 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_tpcds;
    

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

    1
    CREATE DATABASE
    

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

    openGauss=# \q
    gsql -d db_tpcds -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_tpcds=> 

    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_tpcds 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_tpcds=>  CREATE TABLE mytable (firstcol int);
      

      If the DISTRIBUTE BY statement is not used to specify distribution columns, the system automatically specifies the first column as a hash distribution column and prompts you to confirm the operation. If CREATE TABLE is displayed at the end of the returned information, the table has been created.

      1
      2
      3
      NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using 'firstcol' as the distribution column by default.
      HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
      CREATE TABLE
      
    • Run the following command to insert data to the table:
      1
      db_tpcds=> INSERT INTO mytable values (100);
      

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

      1
      INSERT 0 1
      
    • Run the following command to view data in the table:
      1
      2
      3
      4
      5
      db_tpcds=> SELECT * from mytable;
       firstcol 
      ----------
            100
      (1 row)
      

    Note:

    • By default, new database objects, such as the mytable 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 cluster installation, GaussDB queries and processes. 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 storage method, see Planning a Storage Model.