Puoi utilizzare il client a riga di comando PostgreSQL per connetterti a Cloud SQL. Questa pagina descrive come connettere un client psql alla tua istanza Cloud SQL, in esecuzione localmente sul tuo sistema client, su una VM di Compute Engine o in Cloud Shell.
Prima di iniziare
Prima di poter utilizzare un client psql per connetterti all'istanza Cloud SQL, esegui le seguenti operazioni:
Crea un'istanza Cloud SQL, inclusa la configurazione dell'utente
predefinito.
Scarica la distribuzione principale di PostgreSQL per la tua piattaforma dalla
pagina dei download di PostgreSQL.
La distribuzione di base include il client psql.
Installa il database PostgreSQL seguendo le istruzioni riportate nella pagina di download.
Configura l'accesso all'istanza Cloud SQL
Per configurare l'accesso all'istanza:
Dalla macchina client o dall'istanza VM di Compute Engine, utilizza
Qual è il mio IP? per visualizzare
l'indirizzo IP della macchina client.
Copia l'indirizzo IP.
Nella console Google Cloud , vai alla pagina Istanze Cloud SQL.
Connettiti all'istanza Cloud SQL senza
crittografia
Per consentirti di connetterti senza crittografia, l'istanza deve avere la
modalità SSL
impostata su ALLOW_UNENCRYPTED_AND_ENCRYPTED. Nella console Google Cloud , la configurazione equivalente è Consenti il traffico di rete non criptato.
Potresti anche voler creare un file di servizio di connessione per gestire i parametri di connessione, soprattutto se ti connetti a più istanze. Per saperne di più, consulta la
documentazione di PostgreSQL.
Inserisci la password. La password è obbligatoria per Cloud SQL anche
se PostgreSQL supporta la connettività senza password durante l'utilizzo di SSL/TLS.
Puoi verificare che la connessione sia criptata cercando la crittografia
nelle informazioni di connessione:
Connettiti all'istanza Cloud SQL utilizzando SSL/TLS e la verifica del certificato client
Se ssl_mode sull'istanza Cloud SQL è configurato su
TRUSTED_CLIENT_CERTIFICATE_REQUIRED,
devi fornire anche un'identità client verificata quando accedi.
Per connetterti utilizzando i certificati SSL/TLS con la verifica del client, devi disporre di quanto segue:
Un certificato di chiave pubblica client
in un file client-cert.pem.
Una chiave privata client
in un file client-key.pem.
Inoltre, per consentire al client di verificare l'identità del server
per l'autenticazione reciproca, specifica il certificato del server
server-ca.pem.
Fai clic sull'icona di Cloud Shell
verso destra nella barra degli strumenti.
L'inizializzazione di Cloud Shell richiede qualche istante.
Al prompt di Cloud Shell, utilizza il client
integrato per connetterti all'istanza Cloud SQL:
gcloudsqlconnectINSTANCE_ID\
--user=postgres
Inserisci la password.
Il comando gcloud sql connect non supporta la connessione
a un'istanza Cloud SQL utilizzando un IP privato o SSL/TLS. Per connetterti
con la crittografia, installa e utilizza il proxy in Cloud Shell:
Se non conosci Google Cloud, crea un account per valutare le prestazioni di Cloud SQL in scenari reali. I nuovi clienti ricevono anche 300 $ di crediti per l'esecuzione, il test e
il deployment di workload senza costi aggiuntivi.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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)"]]