Setting up PostgreSQL
The following sections describe how to replace the default H2 databases with PostgreSQL:
Setting up the database and login role
Follow the steps below to set up a PostgreSQL database.
- Install PostgreSQL on your computer as follows:
- Start the PostgreSQL service using the following command:
- Create a database and the login role from a GUI using the PGAdminIII tool.
- To connect PGAdminIII to a PostgreSQL database server, locate the server from the object browser, right-click the client, and click Connect. This will show you the databases, tablespaces, and login roles as follows:
- To create a database, click Databases in the tree (inside the object browser), and click New Database.
- In the New Database dialog box, give a name to the database (for example: gregdb) and click OK.
To create a login role, click Login Roles in the tree (inside the object browser), and click New Login Role. Enter the role name and a password.
These values will be used in the product configurations as described in the following sections. In the sample configuration,
gregadmin
will be used as both the role name and the password.- Optionally enter other policies, such as the expiration time for the login and the connection limit.
Click OK to finish creating the login role.
Setting up the drivers
- Download the PostgreSQL JDBC4 driver.
Copy the driver to your WSO2 product's <
PRODUCT_HOME>/repository/components/lib
directory.
Setting up datasource configurations
A datasource is used to establish the connection to a database. By default, WSO2_CARBON_DB
datasource is used to connect to the default H2 database, which stores registry and user management data. After setting up the PostgreSQL database to replace the default H2 database, either change the default configurations of the WSO2_CARBON_DB
datasource, or configure a new datasource to point it to the new database as explained below.
Changing the default WSO2_CARBON_DB datasource
Follow the steps below to change the type of the default WSO2_CARBON_DB
datasource.
Edit the default datasource configuration in the <
PRODUCT_HOME>/repository/conf/datasources/master
-datasources.xml
file as shown below.<datasource> <name>WSO2_CARBON_DB</name> <description>The datasource used for registry and user manager</description> <jndiConfig> <name>jdbc/WSO2CarbonDB</name> </jndiConfig> <definition type="RDBMS"> <configuration> <url>jdbc:postgresql://localhost:5432/gregdb</url> <username>regadmin</username> <password>regadmin</password> <driverClassName>org.postgresql.Driver</driverClassName> <maxActive>80</maxActive> <maxWait>60000</maxWait> <minIdle>5</minIdle> <testOnBorrow>true</testOnBorrow> <defaultAutoCommit>true</defaultAutoCommit> <validationQuery>SELECT 1</validationQuery> <validationInterval>30000</validationInterval> </configuration> </definition> </datasource>
The elements in the above configuration are described below:
Element Description url The URL of the database. The default port for a PostgreSQL instance is 5432. username and password The name and password of the database user. driverClassName The class name of the database driver. maxActive The maximum number of active connections that can be allocated at the same time from this pool. Enter any negative value to denote an unlimited number of active connections. maxWait 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. You can enter zero or a negative value to wait indefinitely. minIdle The minimum number of active connections that can remain idle in the pool without extra ones being created, or enter zero to create none. testOnBorrow
Whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and another attempt will be made to borrow another. validationInterval The indication to avoid excess validation, and only run validation at the most, at this frequency (time in milliseconds). If a connection is due for validation, but has been validated previously within this interval, it will not be validated again. defaultAutoCommit Whether to commit database changes automatically or not. This property is not applicable to the Carbon database in WSO2 products because auto committing is usually handled at the code level, i.e., the default auto commit configuration specified for the RDBMS driver will be effective instead of this property element. Typically, auto committing is enabled for an RDBMS drivers by default.
When auto committing is enabled, each SQL statement will be committed to the database as an individual transaction, as opposed to committing multiple statements as a single transaction.
For more information on other parameters that can be defined in the <
PRODUCT_HOME>/repository/conf/datasources/m
aster-datasources.xml
file, see Tomcat JDBC Connection Pool.
Configuring new datasources to manage registry or user management data
Follow the steps below to configure new datasources to point to the new databases you create to manage registry and/or user management data separately.
Add a new datasource with similar configurations as the
WSO2_CARBON_DB
datasource above to the <PRODUCT_HOME>/repository/conf/datasources/
master-datasources.xml
file. Change its elements with your custom values. For instructions, see Setting up datasource configurations.If you are setting up a separate database to store registry-related data, update the following configurations in the <
PRODUCT_HOME>/repository/conf/
registry.xml
file.<dbConfig name="wso2registry"> <dataSource>jdbc/MY_DATASOURCE_NAME</dataSource> </dbConfig>
If you are setting up a separate database to store user management data, update the following configurations in the <
PRODUCT_HOME>/repository/conf/
user-mgt.xml
file.<Configuration> <Property name="dataSource">jdbc/MY_DATASOURCE_NAME</Property> </Configuration>
Creating database tables
To create the database tables, connect to the database that you created earlier and run the following scripts.
To create tables in the registry and user manager database (
WSO2CARBON_DB
), use the below script:<PRODUCT_HOME>/dbscripts/postgresql.sql
Restart the server.
You can create database tables automatically when starting the product for the first time by using the
-Dsetup
parameter as follows:For Windows:
<PRODUCT_HOME>/bin/wso2server.bat -Dsetup
For Linux:
<PRODUCT_HOME>/bin/wso2server.sh -Dsetup
When WSO2 DAS is set up to use PostgreSQL, you may see the following error in the logs.
duplicate key value violates unique constraint XXX
This occurs when performing insert/overwrite operations. The system first attempts to insert the record, and if a record with the same key value exists, it logs this error before overwriting the record. This is due to the unavailability of a MERGE
statement in PostgreSQL versions that are older than version 9.5, and the performance shortcomings resulting from repeated UPDATE
calls. If you want to disable these logs, uncomment recordMergeQuery
. However, doing so in versions older than 9.5 may result in the failure of WRITE
operations.