Tag Archives: PostgreSQL
pg_archivecleanup to clean old archive logs in PostgreSQL
archive_cleanup_command = ‘pg_archivecleanup archivelocation %r’
pg_archivecleanup [ option … ] archivelocation restartwalfile
Example:-
pg_archivecleanup -d /home/postgres/archive 000000010000003700000010.00000020.backup
This will remove all the logfiles which are older than “000000010000003700000010”
So After completion of the base backup we can use this command to clean the old archivelogs.
connection pooling for postgresql using pgbouncer
- GNU Make 3.81+
- libevent 2.0
- (optional) OpenSSL 1.0.1 for TLS support.
- (optional) c-ares as alternative to libevent’s evdns.
Convert Bytea to text in PostgreSQL
postgres=# create table test(
name bytea);
CREATE TABLE
Query to get all user defined objects in PostgreSQL
SELECT ‘processsuitedb’as “DBNAME”,n.nspname as “SCHEMA_NAME”, c.relname AS “OBJECT_NAME”,relkind as “OBJECT_TYPE”,
CASE c.relkind
WHEN ‘r’ THEN ‘USER_TABLE’
WHEN ‘v’ THEN ‘VIEW’
WHEN ‘m’ THEN ‘VIEW’
WHEN ‘i’ THEN ‘INDEX’
WHEN ‘S’ THEN ‘SEQUENCE’
END as “DESCRIPTION”
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname=’public’ AND n.nspname NOT IN (‘pg_catalog’, ‘pg_toast’, ‘information_schema’)
union
select ‘processsuitedb’as “DBNAME”,n.nspname as “SCHEMA_NAME”,c.conname as “OBJECT_NAME”, ‘c’ as “OBJECT_TYPE”,’CONSTRAINT’ as “DESCRIPTION”
from pg_catalog.pg_constraint c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.connamespace
WHERE n.nspname=’public’ AND n.nspname NOT IN (‘pg_catalog’, ‘pg_toast’, ‘information_schema’)
union
select ‘processsuitedb’as “DBNAME”,n.nspname as “SCHEMA_NAME”,c.proname as “OBJECT_NAME”, ‘f’ as “OBJECT_TYPE”,’FUNCTION’ as “DESCRIPTION”
from pg_catalog.pg_proc c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.pronamespace
WHERE n.nspname=’public’ AND n.nspname NOT IN (‘pg_catalog’, ‘pg_toast’, ‘information_schema’)
union
SELECT ‘processsuitedb’as “DBNAME”,n.nspname as “SCHEMA_NAME”,t.tgname as “OBJECT_NAME”, ‘t’ as “OBJECT_TYPE”,’TRIGGER’ as “DESCRIPTION”
FROM pg_catalog.pg_trigger t
join pg_catalog.pg_class c on t.tgrelid = c.oid
JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
WHERE n.nspname=’public’ AND n.nspname NOT IN (‘pg_catalog’, ‘pg_toast’, ‘information_schema’);
zip archive log files using archive_command postgres
Enable archiving with compression of archivelogs:
wal_level=archive # you can set this value to any value after archive
archive_mode = on
archive_command = ‘cp %p /home/postgres/archivedir/%f && gzip /home/postgres/archivedir/%f’
NOTE: replace “/home/postgres/archivedir” with the archive location where you want to store your archive logs.
ERROR: could not load library "/opt/PostgreSQL/9.6/lib/postgresql/plperl.so": /opt/PostgreSQL/9.6/lib/postgresql/plperl.so: undefined symbol: Perl_save_strlen
PostgreSQL session and object level auditing
Enable auto start of PostgreSQL cluster on Linux machine
PostgreSQL auto start script
#
# chkconfig: 2345 80 05
# description: start and stop PostgreSQL Database Enterprise Edition
#
#
# Note: Change the value of POSTGRES to the login name of the PostgreSQL owner
POSTGRES=postgres
export PGDATA=/home/postgres/DATA
export PGPORT=5432
export PGSOFTWARE=/home/postgres/9.6_SOFTWARE #location where we installed the software
PATH=${PATH}:$PGSOFTWARE/bin
export PATH
case $1 in
‘start’)
echo $”Starting PostgreSQL: “
if [[ `whoami` == ‘postgres’ ]]
then
$PGSOFTWARE/bin/pg_ctl start
else
su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl start”
fi
;;
‘stop’)
echo $”Shutting down PostgreSQL: “
if [[ `whoami` == ‘postgres’ ]]
then
$PGSOFTWARE/bin/pg_ctl start
else
su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl stop”
fi
;;
‘status’)
echo $”Status of PostgreSQL: “
if [[ `whoami` == ‘postgres’ ]]
then
$PGSOFTWARE/bin/pg_ctl status
else
su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl status”
fi
;;
‘restart’)
echo $”Restarting PostgreSQL: “
if [[ `whoami` == ‘postgres’ ]]
then
$PGSOFTWARE/bin/pg_ctl restart
else
su $POSTGRES -c “$PGSOFTWARE/bin/pg_ctl restart”
fi
;;
*)
echo “usage: $0 {start|stop|restart|status}”
exit
;;
esac
exit