DB Link On Different Instances and PG - AUDIT
DB Link On Different Instances and PG - AUDIT
DB Link On Different Instances and PG - AUDIT
[postgres@test-machine02]
[postgres@test-machine02]psql
psql (13.2)
Type "help" for help.
postgres=#
postgres=#
postgres=# create database remote_db;
CREATE DATABASE
postgres=#
postgres=# CREATE USER fdw_user WITH ENCRYPTED PASSWORD 'secret';
CREATE ROLE
postgres=# \du+
List of roles
Role name | Attributes | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------
user_test | Superuser | {} |
fdw_user | | {} |
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |
repluser | Replication | {} |
postgres=#
postgres=# \q
[postgres@test-machine02]psql -p 6432 -U fdw_user remote_db
Password for user fdw_user:
psql (13.2)
Type "help" for help.
remote_db=> create table employee (id int, first_name varchar(20), last_name varchar(20));
CREATE TABLE
remote_db=> insert into employee values (1,'jobin','augustine'),(2,'avinash','vallarapu'),
(3,'fernando','camargos');
INSERT 0 3
remote_db=> \dt employee
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | employee | table | fdw_user
(1 row)
remote_db=>
[postgres@test-machine02]pwd
/var/lib/pgsql/13/data
[postgres@test-machine02]cat pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
host all all 192.168.114.0/24 scram-sha-256
[postgres@test-machine02]
Dependencies Resolved
========================================================================
========================================================================
=============================
Package Arch Version
Repository Size
========================================================================
========================================================================
=============================
Installing:
postgresql13-contrib x86_64 13.4-1PGDG.rhel7
pgdg13 609 k
Updating for dependencies:
postgresql13 x86_64 13.4-1PGDG.rhel7
pgdg13 1.4 M
postgresql13-libs x86_64 13.4-1PGDG.rhel7
pgdg13 381 k
postgresql13-server x86_64 13.4-1PGDG.rhel7
pgdg13 5.4 M
Transaction Summary
========================================================================
========================================================================
=============================
Install 1 Package
Upgrade ( 3 Dependent packages)
Verifying : postgresql13-13.1-1PGDG.rhel7.x86_64
5/7
Verifying : postgresql13-libs-13.1-1PGDG.rhel7.x86_64
6/7
Verifying : postgresql13-server-13.1-1PGDG.rhel7.x86_64
7/7
Installed:
postgresql13-contrib.x86_64 0:13.4-1PGDG.rhel7
Dependency Updated:
postgresql13.x86_64 0:13.4-1PGDG.rhel7 postgresql13-libs.x86_64 0:13.4-
1PGDG.rhel7 postgresql13-server.x86_64 0:13.4-1PGDG.rhel7
Complete!
[root@test-machine01 extension]#
local_db=#
local_db=# create extension dblink;
CREATE EXTENSION
local_db=#
local_db=#
local_db=# \dx
List of installed extensions
Name | Version | Schema | Description
--------------+---------+------------+--------------------------------------------------------------
dblink | 1.2 | public | connect to other PostgreSQL databases from within a database
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgres_fdw | 1.0 | public | foreign-data wrapper for remote PostgreSQL servers
(3 rows)
local_db=#
local_db=#
local_db=# select * from pg_foreign_server;
oid | srvname | srvowner | srvfdw | srvtype | srvversion | srvacl |
srvoptions
-------+----------------------+----------+--------+---------+------------+-------------------------------------------
+--------------------------------------------------
26439 | testmachine02_fdw | 10 | 26438 | | |
{postgres=U/postgres,app_user=U/postgres} | {dbname=remote_db,host=test-
machine02,port=5432}
26492 | testmachine02_dblink | 10 | 26489 | | | |
{dbname=remote_db,host=test-machine02,port=5432}
(2 rows)
local_db=#
local_db=#
local_db=# create user mapping for app_user server testmachine02_dblink options (user
'fdw_user', password 'secret');
CREATE USER MAPPING
local_db=#
local_db=# select * from pg_user_mappings;
umid | srvid | srvname | umuser | usename | umoptions
-------+-------+----------------------+--------+----------+---------------------------------
26440 | 26439 | testmachine02_fdw | 26421 | app_user | {user=fdw_user,password=secret}
26493 | 26492 | testmachine02_dblink | 26421 | app_user | {user=fdw_user,password=secret}
(2 rows)
local_db=#
local_db=#
local_db=# \quit
[postgres@test-machine01]
[postgres@test-machine01] psql -p 5432 -U app_user local_db
Password for user app_user:
psql (9.2.24, server 13.4)
WARNING: psql version 9.2, server version 13.0.
Some psql features might not work.
Type "help" for help.
local_db=>
local_db=> select dblink_connect('conn_dblink','testmachine02_dblink');
dblink_connect
----------------
OK
(1 row)
local_db=> select * from dblink ('conn_dblink','select * from employee') as test_dblink (id int,
first_name varchar(20), last_name varchar(20));
id | first_name | last_name
----+------------+-----------
1 | jobin | augustine
2 | avinash | vallarapu
3 | fernando | camargos
(3 rows)
local_db=>
========================================================================
Auditing==================================================================
==================
shared_preload_libraries = ‘pgaudit’
start cluster
You can run the following query to check the new extension created:
READ Example
First, create a test table and insert some data:
DO $$
BEGIN
RAISE NOTICE 'pgAudit rocks!';
END
$$;
Finally, check pg_log for the audit entries:
$ tail -5 postgresql-Fri.log
With pgAudit set to audit the role class, create, alter and drop some roles:
CHECKPOINT;
VACUUM pgauditExample;
Finally, check pg_log for the audit entries: