Configuring a PostgreSQL Datasource in Apache Tomcat

On this page

Still need help?

The Atlassian Community is here for you.

Ask the community

This page tells you how to set up a PostgreSQL datasource connection for Confluence.

Step 1. Shut down Confluence

  1. Run bin/stop-confluence.sh or bin/stop-confluence.bat to bring down Confluence while you are making these changes.
  2. Make a backup of your <CONFLUENCE_HOME>/confluence.cfg.xml file and your <CONFLUENCE_INSTALLATION>/conf/server.xml file, so that you can easily revert if you have a problem.

On this page:

Related pages:

Step 2. Install the PostgreSQL Server database driver

  1. Download the PostgreSQL Server JDBC driver JAR file. 
    • Links are available on this page: Database JDBC Drivers.
    • Alternatively, you can get the driver from your Confluence installation: /confluence/WEB-INF/lib/postgresql-x.x-x.jdbcx.jar, where 'x' represents a version number.
  2. Copy the driver JAR file to the /lib directory of your Confluence installation directory.

Step 3. Configure Tomcat

  1. Edit the conf/server.xml file in your Confluence installation directory.
  2. Find the following lines:

    <Context path="" docBase="../confluence" debug="0" reloadable="true">
    <!-- Logger is deprecated in Tomcat 5.5. Logging configuration for Confluence is 
    specified in confluence/WEB-INF/classes/log4j.properties -->
  3. Insert the following DataSource Resource element directly after the lines above (inside the Context element, directly after the opening <Context.../> line,  before Manager)

    <!-- If you're using Confluence 5.7 or below; change maxTotal to maxActive -->
    <Resource name="jdbc/confluence" auth="Container" type="javax.sql.DataSource"
              username="postgres"
              password="postgres"
              driverClassName="org.postgresql.Driver"
              url="jdbc:postgresql://localhost:5432/yourDatabaseName"
              maxTotal="60"
              maxIdle="20"
              validationQuery="select 1" /> 
    
    • Replace 'postgres' in the username and password parameters with the actual credentials for your database.
    • In the url parameter, replace 'yourDatabaseName' with the name of the database your Confluence data will be stored in.
    • maxTotal and maxIdle define the number of database connections that will be allowed at one time, and the number that will be kept open when there is no database activity. These can be adjusted based on your requirements. See the Apache Tomcat 8 Datasource documentation for more information.

Notes:

  • If switching from a direct JDBC connection to datasource, you can find the above details in your <CONFLUENCE_HOME>/confluence.cfg.xml file.
  • Here are the configuration properties for Tomcat's standard data source resource factory (org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory):

    • driverClassName — Fully qualified Java class name of the JDBC driver to be used.
    • maxTotal — The maximum number of database connections in the pool at the same time.
    • maxIdle — The maximum number of connections that can sit idle in this pool at the same time.
    • maxWaitMillis — The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception.
    • password — Database password to be passed to our JDBC driver.
    • url — Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.)
    • user — Database username to be passed to our JDBC driver.
    • validationQuery — SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query MUST be an SQL SELECT statement that returns at least one row.
  • Why is the validationQuery element needed? When a database server reboots, or there is a network failure, all the connections in the connection pool are broken and this normally requires a Application Server reboot. However, the Commons DBCP (Database Connection Pool) which is used by the Tomcat application server can validate connections before issuing them by running a simple SQL query, and if a broken connection is detected, a new one is created to replace it. To do this, you will need to set the "validationQuery" option on the database connection pool.

Step 4. Configure the Confluence web application

  1. Edit the confluence/WEB-INF/web.xml file in your Confluence installation directory.
  2. Insert the following element just before </web-app> near the end of the file:

    <resource-ref>
        <description>Connection Pool</description>
        <res-ref-name>jdbc/confluence</res-ref-name>
        <res-type>javax.sql.Datasource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

If you are changing an existing Confluence installation over to using a Tomcat datasource:

  1. Edit the <CONFLUENCE_HOME>/confluence.cfg.xml file.
  2. Delete any line that contains a property that begins with hibernate.
  3. Insert the following at the start of the <properties> section:

    <property name="hibernate.setup"><![CDATA[true]]></property>
    <property name="hibernate.dialect"><![CDATA[net.sf.hibernate.dialect.PostgreSQLDialect]]></property>
    <property name="hibernate.connection.datasource"><![CDATA[java:comp/env/jdbc/confluence]]></property>

Step 5. Restart Confluence

Run bin/start-confluence.sh or bin/start-confluence.bat to start Confluence with the new settings.

Last modified on Mar 10, 2017

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.