Connettere psycopg3 a un database in dialetto PostgreSQL
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina spiega come connettere il driver PostgreSQL psycopg3 a un database con dialetto PostgreSQL in Spanner. psycopg3 è un driver Python per PostgreSQL.
Verifica che PGAdapter sia in esecuzione sulla stessa macchina dell'applicazione che si connette utilizzando il driver psycopg3 PostgreSQL.
importpsycopgwithpsycopg.connect("host=APPLICATION_HOST port=PORT dbname=DATABASE_NAME sslmode=disable")asconn:conn.autocommit=Truewithconn.cursor()ascur:cur.execute("select 'Hello world!' as hello")print("Greeting from Cloud Spanner PostgreSQL:",cur.fetchone()[0])
Sostituisci quanto segue:
APPLICATION_HOST: il nome host o l'indirizzo IP
della macchina in cui è in esecuzione PGAdapter. Se esegui l'app localmente,
utilizza localhost.
PORT: il numero di porta su cui è in esecuzione PGAdapter. Modifica questa impostazione nella stringa di connessione se PGAdapter è
in esecuzione su una porta personalizzata. Altrimenti, utilizza la porta predefinita, 5432.
Socket di dominio Unix
Questa sezione spiega come utilizzare i socket di dominio Unix per connettersi a un database con dialetto PostgreSQL. Utilizza i socket di dominio Unix per la latenza più bassa possibile.
Per utilizzare i socket di dominio Unix, PGAdapter deve essere in esecuzione sullo stesso host dell'applicazione client.
Verifica che il driver JDBC PostgreSQL sia caricato.
importpsycopgwithpsycopg.connect("host=/tmpport=PORTdbname=DATABASE_NAME") as conn:conn.autocommit=Truewithconn.cursor()ascur:cur.execute("select 'Hello world!' as hello")print("Greetings from Cloud Spanner PostgreSQL:",cur.fetchone()[0])
Sostituisci quanto segue:
/tmp: la directory socket di dominio predefinita per
PGAdapter. Puoi modificare questa impostazione utilizzando l'argomento della riga di comando -dir.
PORT: il numero di porta su cui è in esecuzione PGAdapter. Modifica questa impostazione nella stringa di connessione se PGAdapter è
in esecuzione su una porta personalizzata. Altrimenti, utilizza la porta predefinita, 5432.
Per maggiori informazioni sulle opzioni di connessione del driver psycopg3 di PostgreSQL,
consulta Opzioni di connessione
psycopg3 nel repository GitHub di PGAdapter.
[[["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-10 UTC."],[],[],null,["# Connect psycopg3 to a PostgreSQL-dialect database\n\nThis page explains how to connect the [PostgreSQL psycopg3 driver](https://www.psycopg.org/psycopg3/) to\na PostgreSQL-dialect database in Spanner. `psycopg3` is a Python\ndriver for PostgreSQL.\n\n1. Verify that PGAdapter is running on the same machine as the\n application that is connecting using the PostgreSQL psycopg3 driver.\n\n export GOOGLE_APPLICATION_CREDENTIALS=/\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eCREDENTIALS_FILE_PATH\u003c/span\u003e\u003c/var\u003e/credentials.json\n docker pull gcr.io/cloud-spanner-pg-adapter/pgadapter\n docker run \\\n -d -p 5432:5432 \\\n -v ${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:ro \\\n -e GOOGLE_APPLICATION_CREDENTIALS \\\n gcr.io/cloud-spanner-pg-adapter/pgadapter \\\n -p \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_NAME\u003c/span\u003e\u003c/var\u003e -i \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eINSTANCE_NAME\u003c/span\u003e\u003c/var\u003e \\\n -x\n\n For more information, see [Start PGAdapter](/spanner/docs/pgadapter-start).\n2. Connect to PGAdapter using TCP.\n\n import psycopg\n\n with psycopg.connect(\"host=\u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e port=\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e dbname=\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e sslmode=disable\") as conn:\n conn.autocommit = True\n with conn.cursor() as cur:\n cur.execute(\"select 'Hello world!' as hello\")\n print(\"Greeting from Cloud Spanner PostgreSQL:\", cur.fetchone()[0])\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e: the hostname or IP address of the machine where PGAdapter is running. If running locally, use `localhost`.\n - \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: the port number where PGAdapter is running. Change this in the connection string if PGAdapter is running on a custom port. Otherwise, use the default port, `5432`.\n\n### Unix domain sockets\n\nThis section explains how to use Unix domain sockets to connect to a\nPostgreSQL-dialect database. Use Unix domain sockets for the lowest possible latency.\n\nTo use Unix domain sockets, PGAdapter must be running on the\nsame host as the client application.\n\nVerify the PostgreSQL JDBC driver is loaded. \n\n import psycopg\n\n with psycopg.connect(\"host=\u003cvar translate=\"no\"\u003e/tmp\u003c/var\u003e\n port=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePORT\u003c/span\u003e\u003c/var\u003e\n dbname=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDATABASE_NAME\u003c/span\u003e\u003c/var\u003e\") as conn:\n conn.autocommit = True\n with conn.cursor() as cur:\n cur.execute(\"select 'Hello world!' as hello\")\n print(\"Greetings from Cloud Spanner PostgreSQL:\", cur.fetchone()[0])\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003e/tmp\u003c/var\u003e: the default domain socket directory for PGAdapter. This can be changed using the `-dir` command-line argument.\n- \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: the port number where PGAdapter is running. Change this in the connection string if PGAdapter is running on a custom port. Otherwise, use the default port, `5432`.\n\nWhat's next\n-----------\n\n- Learn more about [PGAdapter](/spanner/docs/pgadapter).\n- For more information about PostgreSQL psycopg3 driver connection options, see [psycopg3 Connection\n Options](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/docs/psycopg3.md) in the PGAdapter GitHub repository."]]