Conectar o JDBC a um banco de dados do dialeto PostgreSQL
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
É possível usar o driver JDBC do PostgreSQL ou o do Spanner com um banco de dados do Spanner no dialeto PostgreSQL. Esta página explica como se conectar ao banco de dados com esses drivers.
Para mais informações sobre as opções de conexão do driver JDBC do PostgreSQL, consulte
PGAdapter - Opções de conexão JDBC
no repositório do PGAdapter no GitHub.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-25 UTC."],[],[],null,["# Connect JDBC to a PostgreSQL-dialect database\n\nYou can use either the PostgreSQL JDBC driver or the\nSpanner JDBC driver with a Spanner PostgreSQL-dialect database. This page\nexplains how to connect to your database with these drivers. \n\n### PostgreSQL JDBC driver\n\nThis section explains how to connect the PostgreSQL JDBC driver to a\nPostgreSQL-dialect database in Spanner. JDBC is the standard Java driver for\nPostgreSQL.\n\nIf you use the PostgreSQL JDBC driver, you must use PGAdapter\nto translate between the PostgreSQL network protocol and the\nSpanner network protocol. You can add PGAdapter as a\ndependency and run it in-process with your application.\n\n1. Add PGAdapter and the PostgreSQL JDBC driver as dependencies to your application. \n\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.postgresql\u003c/groupId\u003e\n \u003cartifactId\u003epostgresql\u003c/artifactId\u003e\n \u003cversion\u003e0.50.0\u003c/version\u003e\n \u003c/dependency\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-cloud-spanner-pgadapter\u003c/artifactId\u003e\n \u003cversion\u003e0.50.0\u003c/version\u003e\n \u003c/dependency\u003e\n\n2. Start PGAdapter in-process with your application. \n\n ```java\n OptionsMetadata.Builder builder =\n OptionsMetadata.newBuilder()\n .setProject(\"\u003cvar translate=\"no\"\u003ePROJECT_NAME\u003c/var\u003e\")\n .setInstance(\"\u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e\")\n .setPort(PORT);\n ProxyServer server = new ProxyServer(builder.build());\n server.startServer();\n server.awaitRunning();\n \n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: the port number where PGAdapter is running. Set to `5432` in most cases or `0` to use a dynamically assigned port.\n3. Make sure the PostgreSQL JDBC driver driver is loaded. \n\n ```java\n Class.forName(\"org.postgresql.Driver\");\n\n try (Connection connection =\n DriverManager.getConnection(\"jdbc:postgresql://\u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e/\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e\")) {\n try (ResultSet resultSet =\n connection.createStatement().executeQuery(\"select 'Hello world!' as hello\")) {\n while (resultSet.next()) {\n System.out.printf(\n \"Greetings from Cloud Spanner PostgreSQL: %s\\n\", resultSet.getString(1));\n }\n }\n }\n \n ```\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\n The PGAdapter GitHub repository contains a\n [sample application.](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/java/jdbc)\n\n### Unix domain sockets\n\nThis section explains how to use Unix domain sockets to connect\nPostgreSQL JDBC driver to a PostgreSQL-dialect database. Use Unix domain sockets for the\nlowest possible latency.\n\nTo use Unix domain sockets, PGAdapter must be running on the\nsame host as the client application. \n\n```java\n// Make sure the PG JDBC driver is loaded.\nClass.forName(\"org.postgresql.Driver\");\n\ntry (Connection connection = DriverManager.getConnection(\"jdbc:postgresql://\u003cvar translate=\"no\"\u003eAPPLICATION_HOST\u003c/var\u003e/\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e\"\n + \"?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg\"\n + \"&socketFactoryArg=\u003cvar translate=\"no\"\u003eDIRECTORY_NAME\u003c/var\u003e.s.PGSQL.\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e\")) {\n try (ResultSet resultSet = connection.createStatement().executeQuery(\"select 'Hello world!' as hello\")) {\n while (resultSet.next()) {\n System.out.printf(\"Greeting from Cloud Spanner PostgreSQL: %s\\n\", resultSet.getString(1));\n }\n }\n }\n \n```\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, use `localhost`.\ncommand-line argument. For example, `/tmp`.\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### Spanner JDBC driver\n\nThis section explains how to use the Spanner JDBC driver to\nconnect to a PostgreSQL-dialect database database.\n\n1. Add the Spanner JDBC driver as a dependency to your application. \n\n \u003cdependencyManagement\u003e\n \u003cdependencies\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003elibraries-bom\u003c/artifactId\u003e\n \u003cversion\u003e26.66.0\u003c/version\u003e\n \u003ctype\u003epom\u003c/type\u003e\n \u003cscope\u003eimport\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n \u003c/dependencyManagement\u003e\n\n \u003cdependencies\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-cloud-spanner-jdbc\u003c/artifactId\u003e\n \u003cexclusions\u003e\n \u003cexclusion\u003e\n \u003cgroupId\u003ecom.google.api.grpc\u003c/groupId\u003e\n \u003cartifactId\u003eproto-google-cloud-spanner-executor-v1\u003c/artifactId\u003e\n \u003c/exclusion\u003e\n \u003c/exclusions\u003e\n \u003c/dependency\u003e\n\n2. Use a Spanner JDBC connection URL to connect to the PostgreSQL-dialect database. \n\n ```java\n // Make sure the PostgreSQL JDBC driver is loaded.\n Class.forName(\"org.postgresql.Driver\");\n\n try (Connection connection = DriverManager.getConnection(\n \"jdbc:cloudspanner:/projects/\u003cvar translate=\"no\"\u003ePROJECT_NAME\u003c/var\u003e/instances/\u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e/databases/\u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e\")) {\n try (ResultSet resultSet =\n connection.createStatement().executeQuery(\"select 'Hello world!' as hello\")) {\n while (resultSet.next()) {\n System.out.printf(\n \"Greetings from Cloud Spanner PostgreSQL: %s\\n\", resultSet.getString(1));\n }\n }\n }\n \n ```\n\n The driver automatically detects the SQL dialect of the specified\n database. A dialect parameter in the connection URL is not required.\n\nWhat's next\n-----------\n\n- Learn more about [PGAdapter](/spanner/docs/pgadapter).\n- For more information about PostgreSQL JDBC driver connection options, see [PGAdapter - JDBC Connection Options](https://github.com/GoogleCloudPlatform/pgadapter/blob/postgresql-dialect/docs/jdbc.md) in the PGAdapter GitHub repository."]]