pgx 연결 문자열에서 localhost 및 5432를 데이터베이스 서버 호스트와 포트로 지정합니다. pgx를 사용하려면 연결 문자열의 사용자 이름과 비밀번호가 필요합니다. PGAdapter는 이를 무시합니다.
필요한 경우 PGAdapter가 기본 PostgreSQL 포트(5432)가 아닌 다른 포트에서 리슨하도록 구성되면 포트 번호를 지정합니다.
기본적으로 PGAdapter는 SSL을 사용 중지합니다. pgx는 기본적으로 SSL이 사용 설정된 상태에서 연결을 처음 시도합니다. 왕복이 한 번 더 필요하므로 연결 요청에서 SSL을 중지하면 연결 프로세스가 빨라집니다.
connString:="postgres://uid:pwd@localhost:5432/my-database?sslmode=disable"ctx:=context.Background()conn,err:=pgx.Connect(ctx,connString)iferr!=nil{returnerr}deferconn.Close(ctx)vargreetingstringerr=conn.QueryRow(ctx,"select 'Hello world!' as hello").Scan(&greeting)iferr!=nil{returnerr}fmt.Printf("Greeting from Cloud Spanner PostgreSQL: %v\n",greeting)
[[["이해하기 쉬움","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 pgx to a PostgreSQL-dialect database\n\nThis page explains how to connect the PostgreSQL pgx driver to a PostgreSQL-dialect database\nin Spanner. `pgx` is a Golang driver for PostgreSQL.\n\nVerify that PGAdapter is running on the same machine as the\napplication that is connecting using the PostgreSQL pgx driver.\n\nFor more information, see [Start PGAdapter](/spanner/docs/pgadapter-start).\n\n- `pgx` requires a username and password in the connection string. PGAdapter ignores these.\n- By default, PGAdapter disables SSL. `pgx` by default first tries to connect with SSL enabled. Disabling SSL in the connection request speeds up the connection process, as it requires one fewer round trip.\n\n connString := \"postgres://uid:pwd@\u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e/\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e?sslmode=disable\"\n ctx := context.Background()\n conn, err := pgx.Connect(ctx, connString)\n if err != nil {\n return err\n }\n defer conn.Close(ctx)\n\n var greeting string\n err = conn.QueryRow(ctx, \"select 'Hello world!' as hello\").Scan(&greeting)\n if err != nil {\n return err\n }\n fmt.Printf(\"Greeting from Cloud Spanner PostgreSQL: %v\\n\", greeting)\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 the\nPostgreSQL pgx driver to a PostgreSQL-dialect database. Use Unix domain sockets for the lowest\npossible latency.\n\nTo use Unix domain sockets, PGAdapter must be running on the\nsame host as the client application. \n\n connString := \"host=\u003cvar translate=\"no\"\u003e/tmp\u003c/var\u003e port=\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e database=\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e\"\n ctx := context.Background()\n conn, err := pgx.Connect(ctx, connString)\n if err != nil {\n return err\n }\n defer conn.Close(ctx)\n\n var greeting string\n err = conn.QueryRow(ctx, \"select 'Hello world!' as hello\").Scan(&greeting)\n if err != nil {\n return err\n }\n fmt.Printf(\"Greeting from Cloud Spanner PostgreSQL: %v\\n\", greeting)\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- Learn more about [pgx Connection Options](https://github.com/GoogleCloudPlatform/pgadapter/blob/postgresql-dialect/docs/pgx.md) in the PGAdapter GitHub repository."]]