Using PostgreSQL with Payara Server

Photo of Jonathan Coustick by Jonathan Coustick

PostgreSQL is a fully SQL-Compliant relational database you can use with Payara Server and with no commercial licensing requirements it is well suited for production environments. This blog will show you how to set up a new PostgreSQL installation on Ubuntu and connect to it from Payara Server.

 

To install PostgreSQL on Ubuntu based distributions:

 

sudo apt-get install postgres postgres-client postgresql-contrib

 

Download the JDBC driver from https://jdbc.postgresql.org/download.html and then add it to Payara Server by running:

asadmin> add-library /path/to/download/jdbcdriver.jar`

Login to Postgres with the default account by running the commands:

 

sudo -i -u postgres

psql

 

You now need to set a password for the PostgreSQL user on the database. Do so with:

\password postgres

and then enter the new password when prompted.

 

For full functionality of PostgreSQL, server instrumentation must be enabled, you do this by running the command in Postgres 

CREATE EXTENSION adminpack;

You will notice there are two pools already.  These are the default server pools and are not recommended for production usage. A new connection pool is needed for PostgreSQL, which you can create by clicking the New button.

 

postgreSQL1

 

Now to create a connection pool. For this tutorial the admin console will be used, but this can be done using asadmin commands or web.xml. The pool name is how the connection pool is to be referred to within Payara Server, for this tutorial I will call it postgresqlpool. Set the resource type to javax.sql.DataSource and the database driver vendor to PostgreSQL. Then click Next.

 

postgreSQL2

 

On the next page, scroll down to the bottom where there is the additional properties table. There you must set the following properties in order to be able to connect:

  • serverName – this is the location of the PostgreSQL server. Put in localhost.
  • User – the username for the database. In this case the default account which we edited earlier, postgres.
  • password  the password that you specified earlier. In production environments it is recommended that you use an alias. See Payara Documentation for more details.

You can leave the rest of the fields blank for now.

 

PostgreSQL3

 

You can test the connection by clicking the ping button once the pool has been created. For more information on connection pools see the blog post here.  

 

4 - PostgreSQL Blog.png

 

Then go to Resources → JDBC → JDBC Resources and click on New.

Enter the JNDI name that you wish to use within your applications and set the pool name to be that of the pool set up in the previous step.

 

PostgreSQL4

 

Now, you can inject a reference to a datasource for the PostgreSQL database inside a managed component like this:

@Resource(lookup = "jdbc/postgrespool")
DataSource ds;

Note that you can only use resources in a web container or a managed bean (i.e. CDI beans, EJBs etc.).

 

 

 Payara Server & Payara Micro  Download Here 

 

 

Related Posts

Comments