Sie können den PostgreSQL-Befehlszeilenclient verwenden, um eine Verbindung zu Cloud SQL herzustellen. Auf dieser Seite wird beschrieben, wie Sie einen psql-Client mit Ihrer Cloud SQL-Instanz verbinden, und zwar auf einem lokalen Client, auf einer Compute Engine-VM oder in Cloud Shell.
Hinweise
Führen Sie die folgenden Schritte aus, bevor Sie einen psql-Client verwenden, um eine Verbindung zu Ihrer Cloud SQL-Instanz herzustellen:
Erstellen Sie eine Cloud SQL-Instanz, in der der Standardnutzer konfiguriert ist.
Installieren Sie den psql-Client aus dem Paketmanager:
sudoyuminstallpostgresql
openSUSE
Installieren Sie den psql-Client aus dem Paketmanager:
sudozypperinstallpostgresql
Andere Plattformen
Laden Sie die PostgreSQL Core Distribution für Ihre Plattform von der PostgreSQL-Downloadseite herunter.
Der psql-Client ist in der Core Distribution enthalten.
Installieren Sie die PostgreSQL-Datenbank nach den Anweisungen auf der Downloadseite.
Zugriff auf Ihre Cloud SQL-Instanz konfigurieren
So konfigurieren Sie den Zugriff auf Ihre Instanz:
Rufen Sie auf dem Clientcomputer oder der Compute Engine-VM-Instanz Wie lautet meine IP auf, um die IP-Adresse des Clientcomputers anzuzeigen.
Kopieren Sie diese IP-Adresse.
Wechseln Sie in der Google Cloud Console zur Seite Cloud SQL-Instanzen.
Klicken Sie auf den Instanznamen, um die Seite Übersicht einer Instanz zu öffnen.
Wählen Sie im SQL-Navigationsmenü die Option Verbindung aus.
Wählen Sie den Tab Netzwerk aus.
Klicken Sie im Abschnitt Autorisierte Netzwerke auf Netzwerk hinzufügen und geben Sie die IP-Adresse des Computers an, auf dem der Client installiert ist.
Klicken Sie auf Fertig und dann am Seitenende auf Speichern, um die Änderungen zu speichern.
Unverschlüsselte Verbindung zu Ihrer Cloud SQL-Instanz herstellen
Damit eine Verbindung ohne Verschlüsselung hergestellt werden kann, muss für die Instanz der SSL-Modus auf ALLOW_UNENCRYPTED_AND_ENCRYPTED festgelegt sein. In der Google Cloud Console lautet die entsprechende Konfiguration Unverschlüsselten Netzwerktraffic zulassen.
Es kann außerdem sinnvoll sein, eine Verbindungsdienstdatei zu erstellen, um Ihre Verbindungsparameter zu verwalten, insbesondere wenn Sie zu mehreren Instanzen Verbindungen herstellen. Weitere Informationen finden Sie in der Dokumentation zu PostgreSQL.
Geben Sie das Passwort ein. Das Passwort ist für Cloud SQL obligatorisch, obwohl PostgreSQL eine passwortfreie Verbindung über SSL/TLS unterstützt.
Sie können prüfen, ob die Verbindung verschlüsselt ist. Suchen Sie dazu in den Verbindungsinformationen nach dieser Chiffre:
SSL/TLS-Verbindung zu Ihrer Cloud SQL-Instanz mit Clientzertifikatprüfung herstellen
Wenn ssl_mode auf Ihrer Cloud SQL-Instanz für TRUSTED_CLIENT_CERTIFICATE_REQUIRED konfiguriert ist, müssen Sie bei der Anmeldung auch eine bestätigte Clientidentität angeben.
Für eine Verbindung über SSL/TLS-Zertifikate mit Clientüberprüfung ist Folgendes erforderlich:
Ein Public-Key-Zertifikat des Clients in einer client-cert.pem-Datei
Ein privater Clientschlüssel in einer client-key.pem-Datei
Geben Sie außerdem das Serverzertifikat server-ca.pem an, damit der Client die Identität des Servers für die gegenseitige Authentifizierung überprüfen kann.
Klicken Sie in der Symbolleiste rechts auf das Cloud Shell-Symbol .
Die Cloud Shell-Initialisierung dauert einen Moment.
Stellen Sie über die Cloud Shell-Eingabeaufforderung mit dem integrierten Client eine Verbindung zur Cloud SQL-Instanz her:
gcloudsqlconnectINSTANCE_ID\
--user=postgres
Geben Sie Ihr Passwort ein.
Der Befehl gcloud sql connect unterstützt keine Verbindung zu einer Cloud SQL-Instanz über private IP-Adressen oder SSL/TLS. Zur Herstellung einer verschlüsselten Verbindung müssen Sie den Proxy in Cloud Shell installieren und verwenden:
Wenn Sie mit Google Cloud noch nicht vertraut sind, erstellen Sie ein Konto, um die Leistungsfähigkeit von Cloud SQL in der Praxis sehen und bewerten zu können. Neukunden erhalten außerdem ein Guthaben von 300 $, um Arbeitslasten auszuführen, zu testen und bereitzustellen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[],[],null,["# Connect using a psql client\n\n\u003cbr /\u003e\n\n[MySQL](/sql/docs/mysql/connect-admin-ip \"View this page for the MySQL database engine\") \\| PostgreSQL \\| [SQL Server](/sql/docs/sqlserver/connect-admin-ip \"View this page for the SQL Server database engine\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nYou can use the PostgreSQL command-line client to connect to Cloud SQL. This page describes how to connect a\n[`psql` client](https://www.postgresql.org/docs/current/static/app-psql.html) to your Cloud SQL instance, whether running\nlocally on your client machine, on a Compute Engine VM,\nor in the Cloud Shell.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| **Note:** On Windows, enter these commands before executing `psql` commands: \n| `SET PGCLIENTENCODING=utf-8` \n| `chcp 65001`\n\n\u003cbr /\u003e\n\n| **Note:** For information about connecting a client to a Cloud SQL instance using the Cloud SQL Auth Proxy, see [Connect using the Cloud SQL Auth Proxy](/sql/docs/postgres/connect-auth-proxy). For information about connecting using private IP, see [Configure private IP](/sql/docs/postgres/configure-private-ip).\n\nBefore you begin\n----------------\n\nBefore you can use a `psql` client to connect to your Cloud SQL\ninstance, do the following:\n\n- Create a Cloud SQL instance, including configuring the default\n user.\n\n See [Create instances](/sql/docs/postgres/create-instance) and\n [Set the\n password for the default user account](/sql/docs/postgres/create-manage-users#user-root).\n- Optionally, create a Compute Engine VM instance and then\n connected to the instance using SSH.\n\n See [Create and\n start a VM instance](/compute/docs/instances/create-start-instance),\n [About SSH connections](/compute/docs/instances/ssh),\n or [Connect to\n Windows VMs using RDP](/compute/docs/instances/connecting-to-windows).\n- Determine how you'll connect to your instance.\n\n For the connection options and how to choose from among them, see\n [About connection options](/sql/docs/postgres/connect-overview).\n\n\u003cbr /\u003e\n\nUse a PostgreSQL client on a local machine or a Compute Engine\nVM\n-----------------------------------------------------------------\n\nUsing a `psql` client to connect to your Cloud SQL\ninstance involves three high-level tasks:\n\n1. [Install the client](#install-mysql-client).\n2. [Configure access to your\n Cloud SQL instance](#configure-instance-mysql).\n3. [Connect to your Cloud SQL instance](#connect).\n\n### Install the client\n\nTo install the `psql` client, do the following: \n\n### Debian/Ubuntu\n\nInstall the psql client from the package manager: \n\n```bash\nsudo apt-get update\nsudo apt-get install postgresql-client\n```\n\n### CentOS/RHEL\n\nInstall the psql client from the package manager: \n\n```bash\nsudo yum install postgresql\n```\n\n### openSUSE\n\nInstall the psql client from the package manager: \n\n```bash\nsudo zypper install postgresql\n```\n\n### Other platforms\n\n1. Download the PostgreSQL Core Distribution for your platform from the [PostgreSQL Downloads page](https://www.postgresql.org/download/). \n The Core Distribution includes the psql client.\n2. Install the PostgreSQL database, following the directions on the download page.\n\n### Configure access to your Cloud SQL\ninstance\n\nTo configure access to your instance, do the following:\n\n1. From the client machine or Compute Engine VM instance, use [What's my IP](http://ipv4.whatismyv6.com) to see the IP address of the client machine.\n2. Copy that IP address.\n3. In the Google Cloud console, go to the **Cloud SQL Instances** page.\n\n [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)\n4. To open the **Overview** page of an instance, click the instance name.\n5. Select **Connections** from the SQL navigation menu.\n6. Select the **Networking** tab.\n7. In the **Authorized networks** section, click **Add network** and enter the IP address of the machine where the client is installed. **Note:** The IP address of the instance and the `psql` client IP address you authorize must both be IPv4.\n8. Click **Done** . Then click **Save** at the bottom of the page to save your changes.\n9. Connect to your instance, either [using SSL/TLS](#connect-ssl) or [without encryption (without using SSL/TLS)](#connect).\n\n### Connect to your Cloud SQL instance without\nencryption\n\n| **Caution:** This procedure configures an unencrypted connection to your database. If your database contains sensitive data, then [connect to your instance using SSL/TLS](#connect-ssl).\n\nTo let you connect without encryption, the instance must have\n[SSL mode](/sql/docs/postgres/configure-ssl-instance#enforcing-ssl)\nset to `ALLOW_UNENCRYPTED_AND_ENCRYPTED`. In the Google Cloud console,\nthe equivalent configuration is **Allow unencrypted network traffic**.\n\n\u003cbr /\u003e\n\nFor more information about the SSL/TLS configuration of your instance, see\n[Configure SSL/TLS certificates](/sql/docs/postgres/configure-ssl-instance).\n\nTo connect to your instance, do the following:\n\n1. Confirm that you have [installed the client](#install-mysql-client) and [configured access to your instance](#configure-instance-mysql).\n2. Start the `psql` client: \n\n ```bash\n psql \"sslmode=disable dbname=postgres user=postgres hostaddr=\u003cvar translate=\"no\"\u003eINSTANCE_IP_ADDRESS\u003c/var\u003e\"\n ```\n3. Enter your password.\n4. The psql prompt appears.\n\n### Connect to your Cloud SQL instance using SSL/TLS\n\n| **Note** : If you are using IAM database authentication to log in to the instance, then use the procedure in [Log in using IAM database authentication](/sql/docs/postgres/iam-logins#logging-in-as-a-user) instead.\n\nTo connect to your instance using SSL/TLS and built-in authentication:\n\n1. Start the `psql` client: \n\n ```bash\n psql \"sslmode=require \\\n hostaddr=\u003cvar translate=\"no\"\u003eINSTANCE_IP_ADDRESS\u003c/var\u003e \\\n user=postgres dbname=\u003cvar translate=\"no\"\u003eDB_NAME\u003c/var\u003e\"\n \n ```\n\n For example: \n\n ```\n psql \"sslmode=require \\\n hostaddr=203.12.34.56 \\\n user=postgres dbname=postgres\"\n \n ```\n\n \u003cbr /\u003e\n\n You might also want to create a Connection Service File to manage your\n connection parameters, especially if you are connecting to more than one\n instance. For more information, see the\n [PostgreSQL documentation](https://www.postgresql.org/docs/current/static/libpq-pgservice.html).\n2. Enter the password. The password is mandatory for Cloud SQL even though PostgreSQL supports passwordless connectivity while using SSL/TLS.\n3. You can confirm that the connection is encrypted by looking for the cipher in the connection information: \n\n ```\n SSL connection (cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128)\n ```\n\n#### Connect to your Cloud SQL instance using SSL/TLS and client certificate verification\n\nIf `ssl_mode` on your Cloud SQL instance is configured to\n`TRUSTED_CLIENT_CERTIFICATE_REQUIRED`,\nthen you must also provide a verified client identity when you log in.\n\nTo connect using SSL/TLS certificates with client verification, you need the following:\n\n- A client public key certificate in a **client-cert.pem** file.\n- A client private key in a **client-key.pem** file.\n\nIn addition, to let the client verify the server's identity\nfor mutual authentication, specify the server certificate\n**server-ca.pem**.\nFor example, to start the `psql` client: \n\n```bash\n psql \"sslmode=verify-ca sslrootcert=server-ca.pem \\\n sslcert=client-cert.pem sslkey=client-key.pem \\\n hostaddr=\u003cvar translate=\"no\"\u003eINSTANCE_IP_ADDRESS\u003c/var\u003e \\\n user=postgres dbname=\u003cvar translate=\"no\"\u003eDB_NAME\u003c/var\u003e\"\n \n```\n\nIf you do not have a client certificate and a corresponding\nprivate key, then [create a new client certificate](/sql/docs/postgres/configure-ssl-instance#new-client).\n\nUsing the client in the Cloud Shell\n-----------------------------------\n\n\u003cbr /\u003e\n\nTo connect to a Cloud SQL instance (public IP only):\n\n1. Go to the Google Cloud console. [Go to the Google Cloud console](https://console.cloud.google.com/)\n\n2. Click the Cloud Shell icon towards the right in the toolbar.\n\n The Cloud Shell takes a few moments to initialize.\n3. At the Cloud Shell prompt, use the built-in client to connect to your Cloud SQL instance: \n\n ```bash\n gcloud sql connect INSTANCE_ID \\\n --user=postgres\n ```\n4. Enter your password.\n\n\nThe `gcloud sql connect` command does not support connecting\nto a Cloud SQL instance using private IP, or using SSL/TLS. To connect\nwith encryption, install and use the proxy in the Cloud Shell:\n\n1. [Install the proxy](/sql/docs/postgres/connect-auth-proxy#install-the-cloud-sql-proxy) (Linux 64-bit) in the `/home/USER` directory.\n2. Start the proxy, using gcloud CLI authentication: \u003cbr /\u003e\n\n ```\n ./cloud-sql-proxy INSTANCE_CONNECTION_NAME &\n ```\n\n \u003cbr /\u003e\n\n3. Connect to the database by using the TCP connection:\n\n ```\n psql -U USERNAME --host=127.0.0.1\n ```\n\n \u003cbr /\u003e\n\nWhat's next\n-----------\n\n\u003cbr /\u003e\n\n- Learn about [configuring an instance with a private IP address](/sql/docs/postgres/configure-private-ip).\n- Learn about [options for connecting to your instance from your application](/sql/docs/postgres/connect-overview#external-connection-methods).\n- Learn about how the [`psql` client works\n with SSL](https://www.postgresql.org/docs/current/static/libpq-ssl.html).\n\n\u003cbr /\u003e\n\nTry it for yourself\n-------------------\n\n\nIf you're new to Google Cloud, create an account to evaluate how\nCloud SQL performs in real-world\nscenarios. New customers also get $300 in free credits to run, test, and\ndeploy workloads.\n[Try Cloud SQL free](https://console.cloud.google.com/freetrial)"]]