Here:
<DB_NAME>=myDatabase
<DB_PORT>=5432
<DB_USER>=myUser
<DB_USER_PWD>=myPassword
<SCHEMA_NAME>=mySchema
<DB_CONN_POOL>=postgreSQLPool
<DATA_SRC>=myDatabaseDS
Procedure:
a) Permit firewall glassfish server , postgres port, for example in centos7
#systemctl enable firewalld
#firewall --add-port=<DB_PORT>/tcp --permanent
##systemctl reload firewalld (<---doesn't work)
#firewall-cmd --reload
b) Configure pg_hba.conf for SSL login for the user:
hostnossl all all 0.0.0.0/0 reject
hostssl all postgres 0.0.0.0/0 reject
hostssl <DB_NAME> <USER_NAME> <IR_ADDR> md5
c) Create User, DB, Schema and grant previleges to the user, on required objects, for example:
###CREATE USER
sudo -u postgres psql -p <DB_PORT> -d postgres -c "CREATE USER <USER_NAME> ENCRYPTED PASSWORD 'myUser_pwd';"
###CREATE DB
sudo -u postgres psql -p <DB_PORT> -d postgres -c "CREATE DATABASE <DB_NAME> OWNER <USER_NAME> ENCODING 'UTF-8' TABLESPACE service_ts LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8' LIMIT -1;"
###CREATE SCHEMA
sudo -u postgres psql -p <DB_PORT> -d <DB_NAME> -c "CREATE SCHEMA "<SCHEMA_NAME>";"
###REVOKE PREVILEGES ON <SCHEMA_NAME> from public
sudo -u postgres psql -p <DB_PORT> -d <DB_NAME> -c "REVOKE ALL ON SCHEMA "<SCHEMA_NAME>" FROM "public";"
###GRANT USAGE ON <SCHEMA_NAME> TO <DB_USER>
sudo -u postgres psql -p <DB_PORT> -d <DB_NAME> -c "GRANT USAGE ON SCHEMA "<SCHEMA_NAME>" TO <USER_NAME>;"
###GRANT PREVILEGES ON TABLES OF <SCHEMA_NAME> TO <DB_USER>
sudo -u postgres psql -p <DB_PORT> -d <DB_NAME> -c "GRANT ALL ON TABLES IN SCHEMA "<SCHEMA_NAME>" TO <USER_NAME>;"
###GRANT PREVILEGES ON SEQUENCES OF <SCHEMA_NAME> TO <DB_USER>
sudo -u postgres psql -p <DB_PORT> -d <DB_NAME> -c "GRANT ALL ON SEQUENCES IN SCHEMA "<SCHEMA_NAME>" TO <USER_NAME>;"
###GRANT PREVILEGES ON FUNCTIONS OF <SCHEMA_NAME> TO <DB_USER>
sudo -u postgres psql -p <DB_PORT> -d <DB_NAME> -c "GRANT ALL ON FUNCTIONS IN SCHEMA "<SCHEMA_NAME>" TO <USER_NAME>;"
d) Restart Postgresql to config changes take place
e) Place the correct postgresql jdbc4 jar (corresponding to version of the server used eg. 9.3/9.4/10.1) in the lib folder of the domain ((ie).../glassfish/domains/<DOMAIN_NAME>/lib)
f) Restart Glassfish to config changes take place
use commands:
asadmin create-jdbc-connection-pool --datasourceClassName "org.postgresql.ds.PGConnectionPoolDataSource" --restype "javax.sql.ConnectionPoolDataSource" --property "ServerName=localhost:PortNumber=<DB_PORT>:DatabaseName=<DB_NAME>:User=<DB_USER>:Password=<DB_USER_PWD>" "<DB_CONN_POOL>"
and
asadmin create-jdbc-resource --connectionpoolid "<DB_CONN_POOL>" "jdbc/<DATA_SRC>"