Help Center> GaussDB> Getting Started> Example: Using DAS to Connect to an Instance and Execute SQL Statements
Updated on 2024-05-14 GMT+08:00

Example: Using DAS to Connect to an Instance and Execute SQL Statements

This section describes how to create a pay-per-use GaussDB instance with the minimum specifications and execute basic SQL syntax.

Buying an Instance

  1. Log in to the management console.
  2. Click in the upper left corner and select a region.
  3. Click on the left and choose Databases > GaussDB.
  4. In the navigation pane on the left, choose GaussDB > Instances.
  5. Click Buy DB Instance.
  6. Configure the basic information, such as the billing mode and instance name.

    Figure 1 Billing mode and basic information

  7. Configure instance specifications.

  8. Select a VPC and security group for the instance and configure the database port.

    The VPC you selected must contain sufficient subnets.

  9. Configure the administrator password, enterprise project, and parameter template.

  10. Click Next, confirm the instance information, and click Submit.
  11. Go to the instance list.

    If status of the instance becomes available, the instance has been created.

Connecting to an Instance Through DAS

  1. Log in to the management console.
  2. Click in the upper left corner and select a region.
  3. Click on the left and choose Databases > Data Admin Service.
  4. In the navigation pane on the left, choose Development Tool to go to the login list page.
  5. Click Add Login.

    After the database is created, the root user is added by default. You do not need to create a root user.

  6. Set DB Engine to GaussDB, retain the default value of Source Database, and configure required parameters.

    You are advised to enable Collect Metadata Periodically and Show Executed SQL Statements.

    If a message is displayed indicating that a connection has been established, go to 9.

  7. Click Test Connection.

    If a message is displayed indicating connection successful, continue with the operation. If a message is displayed indicating connection failed and the failure cause is provided, make modifications according to the error message.

  8. Click OK.
  9. Locate the added instance, click Log In in the Operation column.

  10. Go to the SQL Query page.

Getting Started with SQL

  1. Create a database user.

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

    CREATE USER joe WITH PASSWORD "xxxxxxxxx";

    If information similar to the following is displayed, the creation is successful.

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

    For more information about database users, see Users and Permissions.

  2. Create a database.

    CREATE DATABASE db_tpcds;

    If information similar to the following is displayed, the creation is successful.

    Switch to the newly created database in the upper left corner.

  3. Create a table.

    • Run the following command to create a schema:

      CREATE SCHEMA myschema;

    • Create a table named mytable that has only one column. The column name is firstcol and the column type is integer.

      CREATE TABLE myschema.mytable (firstcol int);

    • Insert data to the table.

      INSERT INTO myschema.mytable values (100);

    • View data in the table.

      SELECT * FROM myschema.mytable;

    Note:

    • By default, new database objects, such as the mytable table, are created in the $user schema. For more information about schemas, see Creating and Managing Schemas.
    • For 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 information about instance installation as well as GaussDB queries and processes. You can collect information about the database by querying the system catalogs. For details about querying system catalogs, see Querying a System Catalog.

  4. In the db_tpcds database, run the following statement as user root to grant all permissions of the db_tpcds database to user joe:

    GRANT ALL ON DATABASE db_tpcds TO joe;

    GRANT USAGE ON schema myschema TO joe;

    GRANT ALL ON TABLE myschema.mytable TO joe;

  5. Log in to the db_tpcds database as user joe.

  6. After login, insert data into the table and verify the data.

    INSERT INTO myschema.mytable values (200);

    SELECT * FROM myschema.mytable;