JDBC mit einer Datenbank im PostgreSQL-Dialekt verbinden
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Sie können entweder den PostgreSQL-JDBC-Treiber oder den Spanner-JDBC-Treiber mit einer Spanner-Datenbank mit PostgreSQL-Dialekt verwenden. Auf dieser Seite wird beschrieben, wie Sie mit diesen Treibern eine Verbindung zu Ihrer Datenbank herstellen.
Weitere Informationen zu den Verbindungsoptionen für den PostgreSQL-JDBC-Treiber finden Sie im PGAdapter-GitHub-Repository unter PGAdapter – JDBC Connection Options.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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."]]