[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-06-03(UTC)"],[],[],null,["# Connect psycopg2 to a PostgreSQL-dialect database\n\nThis page explains how to connect the PostgreSQL psycopg2 driver to a\nPostgreSQL-dialect database in Spanner. `psycopg2` is a Python\ndriver for PostgreSQL.\n\nVerify that PGAdapter is running on the same machine as the\napplication that is connecting using the PostgreSQL psycopg2 driver.\n\nFor more information, see [Start PGAdapter](/spanner/docs/pgadapter-start). \n\n connection = psycopg2.connect(database=\"\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e\",\n host=\"\u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e\",\n port=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003ePORT\u003c/span\u003e\u003c/var\u003e)\n\n cursor = connection.cursor()\n cursor.execute('select \\'Hello World\\'')\n for row in cursor:\n print(row)\n\n cursor.close()\n connection.close()\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e: the hostname or IP address of the machine where PGAdapter is running. If running locally, you can 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 database. Use Unix domain socket connections when you need to have\nthe lowest possible latency.\n\nTo use Unix domain sockets, PGAdapter must be running on the\nsame host as the client application. \n\n connection = psycopg2.connect(database=\"\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e\",\n 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\n cursor = connection.cursor()\n cursor.execute('select \\'Hello World\\'')\n for row in cursor:\n print(row)\n\n cursor.close()\n connection.close()\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 psycopg2 driver connection options, see [psycopg2 Connection Options](https://github.com/GoogleCloudPlatform/pgadapter/blob/postgresql-dialect/docs/psycopg2.md) in the PGAdapter GitHub repository."]]