Connecter JDBC à une base de données en dialecte PostgreSQL
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Vous pouvez utiliser le pilote JDBC PostgreSQL ou le pilote JDBC Spanner avec une base de données Spanner utilisant le dialecte PostgreSQL. Cette page explique comment vous connecter à votre base de données avec ces pilotes.
Pour en savoir plus sur les options de connexion du pilote JDBC PostgreSQL, consultez PGAdapter : options de connexion JDBC dans le dépôt GitHub PGAdapter.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/05 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/05 (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."]]