This page shows you how to create and connect to a PostgreSQL instance and perform basic SQL operations by using the Google Cloud console and a client. The resources created in this quickstart typically cost less than a dollar, assuming you complete the steps, including the cleanup, in a timely manner.
Before you begin Note: The name you use for your project must be between 4 and 30 characters. When you type the name, the form suggests a project ID, which you can edit. The project ID must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash.In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Make sure that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Make sure that billing is enabled for your Google Cloud project.
In the Google Cloud console, go to the APIs page.
Enable the Cloud SQL Admin API. gcloudClick the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the gcloud
commands presented throughout this quickstart.
Run the gcloud services enable
command as follows using Cloud Shell to enable the APIs required for this quickstart.:
gcloud services enable sqladmin.googleapis.com
This command enables the following APIs:
Make sure that you have the following role or roles on the project: Cloud SQL Admin (roles/cloudsql.admin
), Cloud SQL Viewer (roles/cloudsql.viewer
)
In the Google Cloud console, go to the IAM page.
Go to IAMIn the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
In the Google Cloud console, go to the IAM page.
Go to IAMIn the New principals field, enter your user identifier. This is typically the email address for a Google Account.
In this quickstart, you use the Google Cloud console. To use the gcloud CLI, cURL, or PowerShell, see Create instances.
In the Google Cloud console, go to the Cloud SQL Instances page.
myinstance
.postgres
user.Click Create instance.
You're returned to the instances list. You can click the new instance right away to see the details, but it won't be available for other operations until it initializes and starts.
Note: In this example, the instance is created using default settings, including a public IP address.In this quickstart, we'll use the psql client in Cloud Shell to connect to your instance. Cloud Shell is a remote, sandboxed environment.
Note: Cloud Shell doesn't work with a private IP address. These instructions are only for an instance with a public IP address.address already in use
.In the Google Cloud console, click the Cloud Shell icon () in the upper right corner.
When Cloud Shell finishes initializing, a message, such as the following one, appears:
Welcome to Cloud Shell! Type "help" to get started. Your Cloud Platform project in this session is set to sample-project. Use "gcloud config set project [PROJECT_ID]" to change to a different project. username@sample-project:~ (sample-project)$
gcloud auth login
command.At the Cloud Shell prompt, connect to your Cloud SQL instance. Use the gcloud sql connect
command as follows. Replace the instance name if your instance name is different.
gcloud sql connect myinstance --user=postgres
The following message appears:
Allowlisting your IP for incoming connection for 5 minutes...done.
This message indicates that the public IP address of your Cloud SQL instance is being allowed to have incoming connections. After this message, you're prompted to enter your password.
Enter your postgres password.
The psql
prompt appears.
CREATE DATABASE guestbook;
\connect guestbook;
CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255), entryID SERIAL PRIMARY KEY); INSERT INTO entries (guestName, content) values ('first guest', 'I got here!'); INSERT INTO entries (guestName, content) values ('second guest', 'Me too!');
SELECT * FROM entries;The result is:
guestname | content | entryid --------------+-------------+--------- first guest | I got here! | 1 second guest | Me too! | 2 (2 rows) postgres=>
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
In the Google Cloud console, go to the Cloud SQL Instances page.
myinstance
instance to open the Instance details page.If you're not using the APIs that were enabled as part of this quickstart, you can disable them.
In the Google Cloud console, go to the APIs page.
Select the Cloud SQL Admin API and then click the Disable API button.
Learn about creating Cloud SQL instances.
Learn about creating PostgreSQL users and databases for your Cloud SQL instance.
See the Cloud SQL pricing information.
In this quickstart you connected to the instance by using Cloud Shell. Learn about all of the connectivity options in Cloud SQL. How you connect depends on your networking configuration, such as if your Cloud SQL instance has a public or private IP address. See how to configure your Cloud SQL instance with a public IP and a private IP address.
Learn about connecting to a Cloud SQL instance from other Google Cloud applications:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4