Creating and managing PostgreSQL databases

This page contains information about creating, listing, and deleting PostgreSQL databases on a Cloud SQL instance.

A newly-created instance has a postgres database.

For more information about PostgreSQL databases, see the PostgreSQL documentation.

Before you begin

Before completing the tasks on this page, you must have:

If you plan to use the psql client to create or manage your databases, you must have:

Creating a database

Follow the rules in Identifiers and Key Words when you create your database name. If not set on creation, new databases have the following default values:

  • Character set: UTF8
  • Collation: en_US.UTF8

To create a database on the Cloud SQL instance:

Console

  1. In the Google Cloud Console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the instance name to open its Overview page.
  3. Select Databases from the SQL navigation menu.
  4. Click Create database.
  5. In the New database dialog, specify the name of the database.
  6. Click Create.

gcloud

For reference information, see gcloud sql databases create.

See the following topics in the PostgreSQL documentation for background and a list of supported character set values:

gcloud sql databases create DATABASE_NAME \
--instance=INSTANCE_NAME \
[--charset=CHARSET] \
[--collation=COLLATION]

REST v1

The following request uses the databases:insert method to create a new database on the specified instance.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID
  • database-name: The name of a database inside the Cloud SQL instance

HTTP method and URL:

POST https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases

Request JSON body:

{
  "project": "project-id",
  "instance": "instance-id",
  "name": "database-name"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

REST v1beta4

The following request uses the databases:insert method to create a new database on the specified instance.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID
  • database-name: The name of a database inside the Cloud SQL instance

HTTP method and URL:

POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases

Request JSON body:

{
  "project": "project-id",
  "instance": "instance-id",
  "name": "database-name"
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

psql Client

For reference information, see CREATE DATABASE in the PostgreSQL documentation.

See the following topics in the PostgreSQL documentation for background and a list of supported character set values:

CREATE DATABASE database_name
  [[ ENCODING encoding ][ LC_COLLATE lc_collate ]];

Listing your databases

To list all databases on an instance:

Console

  1. In the Google Cloud Console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the instance name to open its Overview page.
  3. Click Databases from the left side menu. The Databases page lists databases along with their collation type, character set, and database type.

gcloud

For reference information, see gcloud sql databases list.

gcloud sql databases list \
--instance=INSTANCE_NAME

REST v1

The following request uses the databases:list method to list the databases for an instance.

When you list the databases using the API, you see additional template databases and a system database that are not displayed by the console. You cannot delete or manage the system database.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID

HTTP method and URL:

GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

REST v1beta4

The following request uses the databases:list method to list the databases for an instance.

When you list the databases using the API, you see additional template databases and a system database that are not displayed by the console. You cannot delete or manage the system database.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID

HTTP method and URL:

GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

psql Client

For reference information, see list.

When you list the databases using the psql client, you see additional template databases and a system database that are not displayed by the console. You cannot delete or manage the system database.

\l

Deleting a database

To delete a database on the Cloud SQL instance:

Console

  1. In the Google Cloud Console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the instance name to open its Overview page.
  3. Click Databases from the left side menu.
  4. In the database list, find the database you want to delete and click the trash can icon.
  5. In the Delete database dialog box, enter the name of the database and then click Delete.

gcloud

For reference information, see gcloud sql databases delete.

gcloud sql databases delete DATABASE_NAME \
--instance=INSTANCE_NAME

REST v1

The following request uses the databases:delete method to delete the specified database.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID
  • database-name: The name of a database inside the Cloud SQL instance

HTTP method and URL:

DELETE https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases/database-name

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

REST v1beta4

The following request uses the databases:delete method to delete the specified database.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID
  • database-name: The name of a database inside the Cloud SQL instance

HTTP method and URL:

DELETE https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases/database-name

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

psql Client

For reference information, see DROP DATABASE in the PostgreSQL documentation.

DROP DATABASE [database_name];

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how Cloud SQL performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try Cloud SQL free