Pgjdbc
Postgresql JDBC Driver
Install / Use
/learn @pgjdbc/PgjdbcREADME
PostgreSQL JDBC Driver
PostgreSQL JDBC Driver (PgJDBC for short) allows Java programs to connect to a PostgreSQL database using standard, database independent Java code. Is an open source JDBC driver written in Pure Java (Type 4), and communicates in the PostgreSQL native network protocol.
Status
Supported PostgreSQL and Java versions
The current version of the driver should be compatible with PostgreSQL 8.4 and higher using the version 3.0 of the protocol and Java 8 (JDBC 4.2) or above. Unless you have unusual requirements (running old applications or JVMs), this is the driver you should be using.
PgJDBC regression tests are run against all PostgreSQL versions since 9.1, including "build PostgreSQL from git master" version. There are other derived forks of PostgreSQL but they have not been certified to run with PgJDBC. If you find a bug or regression on supported versions, please file an Issue.
Note: PgJDBC versions since 42.8.0 are not guaranteed to work with PostgreSQL older than 9.1.
Get the Driver
Most people do not need to compile PgJDBC. You can download the precompiled driver (jar) from the PostgreSQL JDBC site or using your chosen dependency management tool:
Maven Central
You can search on The Central Repository with GroupId and ArtifactId org.postgresql:postgresql.
<!-- Add the following dependency to your pom.xml, -->
<!-- replacing LATEST with specific version as required -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>LATEST</version>
</dependency>
Development snapshots
Snapshot builds (builds from master branch) are also deployed to OSS Sonatype Snapshot Repository, so you can test current development version (test some bugfix) by enabling the repository and using the latest SNAPSHOT version.
There are also available (snapshot) binary RPMs in Fedora's Copr repository.
Documentation
For more information you can read the PgJDBC driver documentation or for general JDBC documentation please refer to The Java™ Tutorials.
Driver and DataSource class
| Implements | Class | | ----------------------------------- | ---------------------------------------------- | | java.sql.Driver | org.postgresql.Driver | | javax.sql.DataSource | org.postgresql.ds.PGSimpleDataSource | | javax.sql.ConnectionPoolDataSource | org.postgresql.ds.PGConnectionPoolDataSource | | javax.sql.XADataSource | org.postgresql.xa.PGXADataSource |
Building the Connection URL
The driver recognises JDBC URLs of the form:
jdbc:postgresql:database
jdbc:postgresql:
jdbc:postgresql://host/database
jdbc:postgresql://host/
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/
jdbc:postgresql://?service=myservice
The general format for a JDBC URL for connecting to a PostgreSQL server is as follows, with items in square brackets ([ ]) being optional:
jdbc:postgresql:[//host[:port]/][database][?property1=value1[&property2=value2]...]
where:
- jdbc:postgresql: (Required) is known as the sub-protocol and is constant.
- host (Optional) is the server address to connect. This could be a DNS or IP address, or it could be localhost or 127.0.0.1 for the local computer. To specify an IPv6 address your must enclose the host parameter with square brackets (jdbc:postgresql://[::1]:5740/accounting). Defaults to
localhost. - port (Optional) is the port number listening on the host. Defaults to
5432. - database (Optional) is the database name. Defaults to the same name as the user name used in the connection.
- propertyX (Optional) is one or more option connection properties. For more information see Connection properties.
Logging
PgJDBC uses java.util.logging for logging.
To configure log levels and control log output destination (e.g. file or console), configure your java.util.logging properties accordingly for the org.postgresql logger.
Note that the most detailed log levels, "FINEST", may include sensitive information such as connection details, query SQL, or command parameters.
Connection Properties
In addition to the standard connection parameters the driver supports a number of additional properties which can be used to specify additional driver behaviour specific to PostgreSQL™. These properties may be specified in either the connection URL or an additional Properties object parameter to DriverManager.getConnection.
| Property | Type | Default | Description | |-------------------------------| -- |:-----------------------:|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | user | String | null | The database user on whose behalf the connection is being made. | | password | String | null | The database user's password. | | options | String | null | Specify 'options' connection initialization parameter. | | service | String | null | Specify 'service' name described in pg_service.conf file. References: The Connection Service File and The Password File. 'service' file can provide all properties including 'hostname=', 'port=' and 'dbname='. | | ssl | Boolean | false | Control use of SSL (true value causes SSL to be required) | | sslfactory | String | org.postgresql.ssl.LibPQFactory | Provide a SSLSocketFactory class when using SSL. | | sslfactoryarg (deprecated) | String | null | Argument forwarded to constructor of SSLSocketFactory class.
