Working with Databases-Creating and Managing Databases
Tags: opengauss
Published: 2023-03-13 08:19:02

**Prerequisites**
Only the database system administrator or users granted with database creation permissions can create a database. For details about how to grant database creation permissions to a user, see Managing Users and Their Permissions.

**Background**
**·** openGauss has two default template databases, template0 and template1, and a default user database, Postgres. The default compatible database type of Postgres is O (that is, **DBCOMPATIBILITY** is set to **A**). In this compatible type, empty strings are processed as null values.

**·** **CREATE DATABASE** creates a database by copying a template database (**template0** by default). Do not use a client or any other tools to connect to or to perform operations on the template databases.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuhckw3aa0ztenh5vgyz.png)

**·** A maximum of 128 databases can be created in openGauss.

**·** A database system consists of multiple databases. A client can connect to only one database at a time. Users cannot query data across databases. If one openGauss contains multiple databases, set the -d parameter to specify the database instance to be connected.

**Precautions**
Assume that the database encoding is SQL_ASCII. (You can run the **show server_encoding**; command to query the encoding used for storing data in the current database.) If the database object name contains multi-byte characters (such as Chinese) or if the object name length exceeds the allowed maximum (63 bytes), the database truncates the last byte (not the last character) of the object name. In this case, half characters may appear.

To resolve this problem, you need to:

**·** Ensure that the name of the data object does not exceed the maximum length.
**·** Change the default database storage code set **(server_encoding)** to UTF-8.
**·** Exclude multi-byte characters from object names.
**·** Ensure that no more than 128 databases are created (recommended).
**·** If you fail to delete an object by specifying its name after truncation, specify its original name to delete it, or manually delete it from the system catalogs on each node.

**Procedure**
1. Create a database.

1.1 Run the following command to create a tablespace named tpcds_local:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0b7g3xiamgqwu0povwj.png)

1.2 Run the following command to create a database named db_tpcc:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0obktrxa8azl3jta5sq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epb6cvpxlsofmrw0w1sr.png)

2. View databases.

2.1 Run the \l meta-command to view the database list of the database system.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghlf6m17978wkcvgdjkp.png)

2.2 Run the following command to query the database list in the pg_database system catalog:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rba1cr3ofw5s9ku7byrt.png)

3. Modify the database.

You can modify database configuration such as the database owner, name, and default settings.

3.1 Run the following command to set the default search path for the database:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwlj5u3mwbj5urgvseun.png)

3.2 Run the following command to modify the database tablespaces:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idlbzfj7uel6fw9nf46t.png)

3.3 Run the following command to rename the database:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pf2udzozoftoqqsadd8.png)

4. Delete the database.

You can run the DROP DATABASE command to delete a database. This command deletes the system catalog of the database and the database directory on the disk. Only the database owner or system administrator can delete a database. A database accessed by users cannot be deleted. You need to connect to another database before deleting this database.

Run the following command to delete the database:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa3i9by6zuy99zi0xeai.png)