This page explains how to connect psql to a PostgreSQL-dialect database in Spanner.
psql is the command-line frontend to PostgreSQL.
You can connect psql to Spanner in two ways:
- Using Spanner PG Connector (
spgc):spgcis a standalone CLI wrapper that bundles PGAdapter and a minimal Java runtime. It starts and stops PGAdapter in the background when you runpsql. - Using a running PGAdapter instance: start and manage the
PGAdapter proxy process separately. Then connect
psqltolocalhost.
Connect using Spanner PG Connector (spgc)
spanner-pg-connector (aliased as spgc) lets you run psql without
installing Java or Docker or manually managing a separate PGAdapter
process.
Install
psqlon yourPATH, and installspgcby following the instructions in Start PGAdapter.Configure Application Default Credentials (ADC), for example, by running
gcloud auth application-default loginor settingGOOGLE_APPLICATION_CREDENTIALS.Run
psqlthroughspgcby specifying the full database resource name:spgc psql -d "projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID"Alternatively, set
GOOGLE_CLOUD_PROJECTandSPANNER_INSTANCE(and optionallySPANNER_DATABASE):export GOOGLE_CLOUD_PROJECT=PROJECT_ID export SPANNER_INSTANCE=INSTANCE_ID spgc psql -d DATABASE_IDOptional: Verify that
psqlconnected to a PostgreSQL-dialect database by running the following query:SELECT 1::bigint;This query is incompatible with GoogleSQL-dialect databases.
Connect to a running PGAdapter instance
If you're running PGAdapter as a separate process or container:
Start PGAdapter on the same machine as the
psqlcommand.For more information, see Start PGAdapter.
In a terminal window, start
psqland specifylocalhostas the database server host.- Optionally specify a port number if PGAdapter is configured to listen on a port other than the default PostgreSQL port (5432).
- Optionally specify one or more commands to send to the database or driver
(for example, the JDBC driver). For each occurrence of the
-coption, you can specify a single command or a batch of commands separated by semicolons (;). For more information, see psql command-line tool.
psql -h localhost [-p PORT] [-c "COMMAND; ..."] ...Optional: Verify that
psqlsuccessfully connected to a PostgreSQL-dialect database by submitting the following query:SELECT 1::bigint;This query is incompatible with GoogleSQL-dialect databases.
What's next
- Learn about PGAdapter.
- See the supported
psqlcommands in psql command-line tool.