SlideShare a Scribd company logo
PG- Database
Administration
PostgreSQL
Database Administrator- Part3
By Poguttu
How Does PostgreSQL Administration Work?
• We can access the PostgreSQL database from localhost or any host. We need to define the IP address of that host in pg_hba.conf file.
• The user is connected to the database using authentication methods. If authentication is successful then the user connected to a specific database otherwise it will disconnect with
an error.
• After authentication user have access to the database and execute the query on which the user has access.
• If a user does not have access to execute the specified query it will throw an error that “Permission Denied”.
• The user also connects through peer authentication in PostgreSQL, it will access through OS user authentication. But in this case, we need both the user to have the same name.
• Below is the PostgreSQL configuration files are as follows.
• Postgresql.conf
• Pg_hba.conf
• postgresql.auto.auto.conf
• pg_ident.conf
• There are multiple methods of authentication used in PostgreSQL like trust, MD5, LDAP, password, SSPI, Kerberos, ident, peer radius, certificate, PAM.
• These methods have different authentication algorithms for each, basically, we have used MD5 authentication methods to authenticate the database from users in PostgreSQL
Roles of PostgreSQL Administration
Below are the roles of PostgreSQL administration.
• Roles and users are very important in PostgreSQL to administer the database. User has default login privileges to the
database. Roles do not have default login privileges to the database.
• We can consider the role as a group in PostgreSQL.
• PostgreSQL roles are very important to administer the PostgreSQL database.
• Roles and users are used in PostgreSQL administration to authenticate with the database.
• There are multiple methods of authentication that have used to authenticate the database from the user.
• PostgreSQL administration is used to authenticate the database from unauthenticated access, PostgreSQL
administration is very important to give appropriate access to the database, tables, and all the objects.
• We can see PostgreSQL users and roles by using the following command. The default administrative user is postgres.
• Postgres is a default administrative user which has full access and grant on the database.
Level of Security
pg_hba.conf - Acces Control
Default setting of pg_hba.conf file as follows
Application Access
PostgreSQL - How to grant access to users?
1.How to grant access to users in PostgreSQL?
Here are some common statement to grant access to a PostgreSQL user:
1. Grant CONNECT to the database:
GRANT CONNECT ON DATABASE database_name TO username;
2. Grant USAGE on schema:
GRANT USAGE ON SCHEMA schema_name TO username;
3. Grant on all tables for DML statements: SELECT, INSERT, UPDATE, DELETE:
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema_name TO username;
4. Grant all privileges on all tables in the schema:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO username;
5. Grant all privileges on all sequences in the schema:
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name TO username;
6. Grant all privileges on the database:
GRANT ALL PRIVILEGES ON DATABASE database_name TO username;
7. Grant permission to create database:
ALTER USER username CREATEDB;
8. Make a user superuser:
ALTER USER myuser WITH SUPERUSER;
9. Remove superuser status:
ALTER USER username WITH NOSUPERUSER;
Those statements above only affect the current existing tables. To apply to newly created tables, you need to use alter default. For example:
ALTER DEFAULT PRIVILEGESFOR USER usernameIN SCHEMA schema_nameGRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO username;
Authentication Problems
Row Level Security (RLS)
Row Level Security (RLS)
Postgres
Monitoring /
Audit tools:- 1.PgBadger: A fast PostgreSQL log analyzer
2.PgCluu: PostgreSQL and system performances monitoring and auditing tool
3.Powa: PostgreSQL Workload Analyzer. Gathers performance stats and provides real-time charts
and graphs to help monitor and tune your PostgreSQL servers. Similar to Oracle AWR.
4.PgObserver: monitor performance metrics of different PostgreSQL clusters.
5.OPM: Open PostgreSQL Monitoring. Gather stats, display dashboards and send warnings
when something goes wrong. Tend to be similar to Oracle Grid Control.
6.check_postgres: script for monitoring various attributes of your database. It is designed
to work with Nagios, MRTG, or in standalone scripts.
7.Pgwatch: monitor PostgreSQL databases and provides a fast and efficient overview
of what is really going on.
8.pgAgent:pgAgent is a job scheduler for PostgreSQL which may be managed using pgAdmin. Prior
to pgAdmin v1.9, pgAgent shipped as part of pgAdmin. From pgAdmin v1.9 onwards, pgAgent is
shipped as a separate application.
9.pgbouncer:PgBouncer is a lightweight connection pooler for PostgreSQL.It contains the
connection pooler and it is used to establish connection between application and database
Table Info
select table_catalog, table_schema, table_name, table_type from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema');
select * from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema') and table_schema not like 'pg_toast%';
Table Activity SELECT * FROM pg_stat_all_tables; Table Block Activity SELECT * FROM pg_statio_all_tables;
How to find the largest table in the postgreSQL database?
SELECT relname, relpages FROM pg_class ORDER BY relpages DESC;
SELECT relname, relpages FROM pg_class ORDER BY relpages DESC limit 1;
Object List
select nsp.nspname as object_schema,
cls.relname as object_name, rol.rolname as owner, case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type from pg_class cls join pg_roles rol on rol.oid = cls.relowner join pg_namespace nsp on nsp.oid = cls.relnamespace where nsp.nspname not in
('information_schema', 'pg_catalog’) and nsp.nspname not like 'pg_toast%’ order by nsp.nspname, cls.relname;
--and rol.rolname = current_user --- remove this if you want to see all objects
Current User objects
SELECT * FROM pg_tables t WHERE t.tableowner = current_user;
SELECT relname as "Table", pg_size_pretty(pg_total_relation_size(relid)) As "Size", pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "External Size“ FROM
pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;
Size of the Objects
SELECT relname AS objectname, relkind AS objecttype, reltuples AS "#entries", pg_size_pretty(relpages::bigint*8*1024) AS size FROM pg_class WHERE relpages >= 8
ORDER BY relpages DESC;
Handy queries
List procedure/function
SELECT * FROM pg_proc WHERE proname='__procedurename__';
List view (including the definition)
SELECT * FROM pg_views WHERE viewname='__viewname__';
Show DB table space in use
SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));
Show DB space in use
SELECT pg_size_pretty(pg_database_size('__database_name__'));
SELECT pg_database_size(current_database());
SELECT pg_database_size('postgres');
select pg_size_pretty(pg_database_size(current_database()));
Show current user's statement timeout
show statement_timeout;
Show table indexes
SELECT * FROM pg_indexes WHERE tablename ='__table_name__' AND schemaname='__schema_name__';
Get all indexes from all tables of a schema:
SELECT t.relname AS table_name, i.relname AS index_name, a.attname AS column_name FROM pg_class t, pg_class i, pg_index ix, pg_attribute a, pg_namespace n WHERE
t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)AND t.relnamespace = n.oid AND n.nspname = 'kartones' ORDER BY t.relname,
i.relname;
•Execution data:
o Queries being executed at a certain DB:
SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query FROM pg_stat_activity WHERE
datname='__database_name__';
•Get all queries from all dbs waiting for data (might be hung):
SELECT * FROM pg_stat_activity WHERE waiting='t'
•Currently running queries with process pid:
SELECT pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_activity(s.backendid) AS current_query
FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
Casting:
•CAST (column AS type) or column::type
•'__table_name__'::regclass::oid: Get oid having a table name
Query analysis:
•EXPLAIN __query__: see the query plan for the given query
•EXPLAIN ANALYZE __query__: see and execute the query plan for the given query
•ANALYZE [__table__]: collect statistics
Keyboard shortcuts
• CTRL + R: reverse-i-search
Tools
•ptop and pg_top: top for PG. Available on the APT repository from apt.postgresql.org.
•pg_activity: Command line tool for PostgreSQL server activity monitoring.
•Unix-like reverse search in psql:
$ echo "bind "^R" em-inc-search-prev" > $HOME/.editrc
$ source $HOME/.editrc
Find Out Essential Settings in a Postgres DB
show config_file;
show effective_cache_size;
show shared_buffers;
show all;
How to calculate postgreSQL database size in disk ?
SELECT pg_database_size('postgres');
SELECT pg_size_pretty(pg_database_size('postgres'));
How to calculate postgreSQL table size in disk ?
SELECT pg_size_pretty(pg_total_relation_size('k1'));
SELECT pg_size_pretty(pg_relation_size('k1'));
select pg_relation_size('audit_hist');
How to view the indexes of an existing postgreSQL table ?
Syntax: # d k1
d pg_attribute
Useful command :
# l : to list all databases
# ? : to list help for all commands
# h create table : show help with topics
# d : to list all tables
# df : to list all functions
# da : to list all aggregate functions
# dn : to list all schemas
# du : to list all users/privileges
# r : to reset the query buffer
# db : to list all table space
dt – table list
dv views
d+ students – table description
dy: List events
di: List indexes
dT+: List all data types
PostgreSQL Scripts
Table Count SELECT table_schema as Schema_Name ,count(*) as Table_Count FROM information_schema.tables WHERE table_schema IN ('testdb') and table_type='BASE TABLE' group by table_schema
Table List & Row
count
SELECT nspname AS schemaname,relname as Tablename ,reltuples as numRows
FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname IN (‘testdb') AND relkind='r’ ORDER BY relname;
Column list select table_schema, table_name, ordinal_position as position, column_name,
data_type, case when character_maximum_length is not null
then character_maximum_length
else numeric_precision end as max_length, is_nullable, column_default as default_value from information_schema.columns where table_schema in ('testdb')
order by table_schema, table_name, ordinal_position;
Constraint
list
select
tc.constraint_schema as Owner, tc.table_name as TableName, kcu.column_name as ColumnName, tc.constraint_name,
tc.constraint_type
from
information_schema.table_constraints as tc
join information_schema.key_column_usage as kcu on (tc.constraint_name = kcu.constraint_name and tc.table_name = kcu.table_name)
join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name where tc.constraint_schema = 'testdb'
--- No search CONDITION in Postgresql
Indexes list SELECT indexname,tablename FROM pg_indexes WHERE schemaname = 'testdb' ORDER BY tablename, indexname;
Partition list (Table
Partition)
select c.relnamespace::regnamespace::text as schema, c.relname as table_name,
pg_get_partkeydef(c.oid) as partition_key from pg_class c
where c.relkind = 'p' and c.relnamespace::regnamespace::text='testdb';
Function &
Procedure
List
SELECT routine_name as objects FROM information_schema.routines
WHERE routine_type='FUNCTION' AND specific_schema IN ('testdb') and routine_name not like '%$%'
Triggers list select trigger_schema, trigger_name from information_schema.triggers
where trigger_schema='testdb' order by trigger_name;
Views list select table_schema as schema_name, table_name as view_name from information_schema.views where table_schema in ('testdb') order by schema_name, view_name;
User Lists select s.nspname as table_schema, s.oid as schema_id, u.usename as owner
from pg_catalog.pg_namespace s join pg_catalog.pg_user u on u.usesysid = s.nspowner
where nspname IN (testdb') order by table_schema;
lists all
materialized
views
select schemaname as schema_name, matviewname as view_name, matviewowner as owner, ispopulated as is_populated, definition
from pg_matviews where schemaname='testdb_schema'order by schema_name, view_name;
lists tables with
primary key
columns
select kcu.table_schema, kcu.table_name, tco.constraint_name,
kcu.ordinal_position as position, kcu.column_name as key_column
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on kcu.constraint_name = tco.constraint_name
and kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
where tco.constraint_type = 'PRIMARY KEY' and kcu.table_schema='testdb_schema'
order by kcu.table_schema, kcu.table_name, position;
Sequence SELECT sequence_schema,sequence_name FROM information_schema.sequences where sequence_schema=‘testdb';
Foreign
Constraints
SELECT
tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name,
ccu.table_schema AS foreign_table_schema, ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name =
tc.constraint_name AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY' and tc.table_schema=‘testdb'
DB LINK SELECT PRONAME,PRONAMESPACE,PROOWNER FROM pg_catalog.pg_proc
WHERE prosrc ILIKE '%dblink%';

More Related Content

What's hot

PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
Command Prompt., Inc
 
What is new in PostgreSQL 14?
What is new in PostgreSQL 14?What is new in PostgreSQL 14?
What is new in PostgreSQL 14?
Mydbops
 
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015PostgreSQL-Consulting
 
Parallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDBParallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDB
Mydbops
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
Alexey Bashtanov
 
Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming Replication
Alexey Lesovsky
 
Postgresql Database Administration Basic - Day1
Postgresql  Database Administration Basic  - Day1Postgresql  Database Administration Basic  - Day1
Postgresql Database Administration Basic - Day1
PoguttuezhiniVP
 
Tuning Autovacuum in Postgresql
Tuning Autovacuum in PostgresqlTuning Autovacuum in Postgresql
Tuning Autovacuum in Postgresql
Mydbops
 
Logical replication with pglogical
Logical replication with pglogicalLogical replication with pglogical
Logical replication with pglogical
Umair Shahid
 
High Availability PostgreSQL with Zalando Patroni
High Availability PostgreSQL with Zalando PatroniHigh Availability PostgreSQL with Zalando Patroni
High Availability PostgreSQL with Zalando Patroni
Zalando Technology
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresql
botsplash.com
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
Command Prompt., Inc
 
MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바
NeoClova
 
Optimizing MariaDB for maximum performance
Optimizing MariaDB for maximum performanceOptimizing MariaDB for maximum performance
Optimizing MariaDB for maximum performance
MariaDB plc
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
An Overview to MySQL SYS Schema
An Overview to MySQL SYS Schema An Overview to MySQL SYS Schema
An Overview to MySQL SYS Schema
Mydbops
 
An Introduction to MongoDB Ops Manager
An Introduction to MongoDB Ops ManagerAn Introduction to MongoDB Ops Manager
An Introduction to MongoDB Ops Manager
MongoDB
 
PostgreSQL Replication Tutorial
PostgreSQL Replication TutorialPostgreSQL Replication Tutorial
PostgreSQL Replication Tutorial
Hans-Jürgen Schönig
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to ask
Carlos Abalde
 

What's hot (20)

PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
What is new in PostgreSQL 14?
What is new in PostgreSQL 14?What is new in PostgreSQL 14?
What is new in PostgreSQL 14?
 
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
How does PostgreSQL work with disks: a DBA's checklist in detail. PGConf.US 2015
 
Parallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDBParallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDB
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming Replication
 
Postgresql Database Administration Basic - Day1
Postgresql  Database Administration Basic  - Day1Postgresql  Database Administration Basic  - Day1
Postgresql Database Administration Basic - Day1
 
Tuning Autovacuum in Postgresql
Tuning Autovacuum in PostgresqlTuning Autovacuum in Postgresql
Tuning Autovacuum in Postgresql
 
Logical replication with pglogical
Logical replication with pglogicalLogical replication with pglogical
Logical replication with pglogical
 
High Availability PostgreSQL with Zalando Patroni
High Availability PostgreSQL with Zalando PatroniHigh Availability PostgreSQL with Zalando Patroni
High Availability PostgreSQL with Zalando Patroni
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresql
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 
MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바
 
Optimizing MariaDB for maximum performance
Optimizing MariaDB for maximum performanceOptimizing MariaDB for maximum performance
Optimizing MariaDB for maximum performance
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
 
An Overview to MySQL SYS Schema
An Overview to MySQL SYS Schema An Overview to MySQL SYS Schema
An Overview to MySQL SYS Schema
 
An Introduction to MongoDB Ops Manager
An Introduction to MongoDB Ops ManagerAn Introduction to MongoDB Ops Manager
An Introduction to MongoDB Ops Manager
 
PostgreSQL Replication Tutorial
PostgreSQL Replication TutorialPostgreSQL Replication Tutorial
PostgreSQL Replication Tutorial
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to ask
 

Similar to Postgresql Database Administration- Day3

Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2
PoguttuezhiniVP
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and Alerting
Grant Fritchey
 
Postgresql
PostgresqlPostgresql
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Ontico
 
Perl Programming - 04 Programming Database
Perl Programming - 04 Programming DatabasePerl Programming - 04 Programming Database
Perl Programming - 04 Programming Database
Danairat Thanabodithammachari
 
Sqlmap
SqlmapSqlmap
Postgresql Database Administration- Day4
Postgresql Database Administration- Day4Postgresql Database Administration- Day4
Postgresql Database Administration- Day4
PoguttuezhiniVP
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
priya951125
 
Migrating To PostgreSQL
Migrating To PostgreSQLMigrating To PostgreSQL
Migrating To PostgreSQL
Grant Fritchey
 
SQLMAP Tool Usage - A Heads Up
SQLMAP Tool Usage - A  Heads UpSQLMAP Tool Usage - A  Heads Up
SQLMAP Tool Usage - A Heads Up
Mindfire Solutions
 
Honey I Shrunk the Database
Honey I Shrunk the DatabaseHoney I Shrunk the Database
Honey I Shrunk the DatabaseVanessa Hurst
 
Neo4j 4.1 overview
Neo4j 4.1 overviewNeo4j 4.1 overview
Neo4j 4.1 overview
Neo4j
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
Vivek Bhusal
 
Pentest Application With GraphQL | Null Bangalore Meetup
Pentest Application With GraphQL | Null Bangalore Meetup Pentest Application With GraphQL | Null Bangalore Meetup
Pentest Application With GraphQL | Null Bangalore Meetup
Divyanshu
 
Creating PostgreSQL-as-a-Service at Scale
Creating PostgreSQL-as-a-Service at ScaleCreating PostgreSQL-as-a-Service at Scale
Creating PostgreSQL-as-a-Service at Scale
Sean Chittenden
 
Tutorial all pp_pg_admin_backup_restore
Tutorial all pp_pg_admin_backup_restoreTutorial all pp_pg_admin_backup_restore
Tutorial all pp_pg_admin_backup_restore
Ganesh Sawant
 

Similar to Postgresql Database Administration- Day3 (20)

Postgre sql unleashed
Postgre sql unleashedPostgre sql unleashed
Postgre sql unleashed
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and Alerting
 
Postgresql
PostgresqlPostgresql
Postgresql
 
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
 
Perl Programming - 04 Programming Database
Perl Programming - 04 Programming DatabasePerl Programming - 04 Programming Database
Perl Programming - 04 Programming Database
 
Sqlmap
SqlmapSqlmap
Sqlmap
 
Postgresql Database Administration- Day4
Postgresql Database Administration- Day4Postgresql Database Administration- Day4
Postgresql Database Administration- Day4
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
 
Migrating To PostgreSQL
Migrating To PostgreSQLMigrating To PostgreSQL
Migrating To PostgreSQL
 
SQLMAP Tool Usage - A Heads Up
SQLMAP Tool Usage - A  Heads UpSQLMAP Tool Usage - A  Heads Up
SQLMAP Tool Usage - A Heads Up
 
Honey I Shrunk the Database
Honey I Shrunk the DatabaseHoney I Shrunk the Database
Honey I Shrunk the Database
 
Neo4j 4.1 overview
Neo4j 4.1 overviewNeo4j 4.1 overview
Neo4j 4.1 overview
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
Pentest Application With GraphQL | Null Bangalore Meetup
Pentest Application With GraphQL | Null Bangalore Meetup Pentest Application With GraphQL | Null Bangalore Meetup
Pentest Application With GraphQL | Null Bangalore Meetup
 
Creating PostgreSQL-as-a-Service at Scale
Creating PostgreSQL-as-a-Service at ScaleCreating PostgreSQL-as-a-Service at Scale
Creating PostgreSQL-as-a-Service at Scale
 
Tutorial all pp_pg_admin_backup_restore
Tutorial all pp_pg_admin_backup_restoreTutorial all pp_pg_admin_backup_restore
Tutorial all pp_pg_admin_backup_restore
 

Recently uploaded

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

Postgresql Database Administration- Day3

  • 2. How Does PostgreSQL Administration Work? • We can access the PostgreSQL database from localhost or any host. We need to define the IP address of that host in pg_hba.conf file. • The user is connected to the database using authentication methods. If authentication is successful then the user connected to a specific database otherwise it will disconnect with an error. • After authentication user have access to the database and execute the query on which the user has access. • If a user does not have access to execute the specified query it will throw an error that “Permission Denied”. • The user also connects through peer authentication in PostgreSQL, it will access through OS user authentication. But in this case, we need both the user to have the same name. • Below is the PostgreSQL configuration files are as follows. • Postgresql.conf • Pg_hba.conf • postgresql.auto.auto.conf • pg_ident.conf • There are multiple methods of authentication used in PostgreSQL like trust, MD5, LDAP, password, SSPI, Kerberos, ident, peer radius, certificate, PAM. • These methods have different authentication algorithms for each, basically, we have used MD5 authentication methods to authenticate the database from users in PostgreSQL
  • 3. Roles of PostgreSQL Administration Below are the roles of PostgreSQL administration. • Roles and users are very important in PostgreSQL to administer the database. User has default login privileges to the database. Roles do not have default login privileges to the database. • We can consider the role as a group in PostgreSQL. • PostgreSQL roles are very important to administer the PostgreSQL database. • Roles and users are used in PostgreSQL administration to authenticate with the database. • There are multiple methods of authentication that have used to authenticate the database from the user. • PostgreSQL administration is used to authenticate the database from unauthenticated access, PostgreSQL administration is very important to give appropriate access to the database, tables, and all the objects. • We can see PostgreSQL users and roles by using the following command. The default administrative user is postgres. • Postgres is a default administrative user which has full access and grant on the database.
  • 6. Default setting of pg_hba.conf file as follows
  • 8. PostgreSQL - How to grant access to users? 1.How to grant access to users in PostgreSQL? Here are some common statement to grant access to a PostgreSQL user: 1. Grant CONNECT to the database: GRANT CONNECT ON DATABASE database_name TO username; 2. Grant USAGE on schema: GRANT USAGE ON SCHEMA schema_name TO username; 3. Grant on all tables for DML statements: SELECT, INSERT, UPDATE, DELETE: GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema_name TO username; 4. Grant all privileges on all tables in the schema: GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO username; 5. Grant all privileges on all sequences in the schema: GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name TO username; 6. Grant all privileges on the database: GRANT ALL PRIVILEGES ON DATABASE database_name TO username; 7. Grant permission to create database: ALTER USER username CREATEDB; 8. Make a user superuser: ALTER USER myuser WITH SUPERUSER; 9. Remove superuser status: ALTER USER username WITH NOSUPERUSER; Those statements above only affect the current existing tables. To apply to newly created tables, you need to use alter default. For example: ALTER DEFAULT PRIVILEGESFOR USER usernameIN SCHEMA schema_nameGRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO username;
  • 12. Postgres Monitoring / Audit tools:- 1.PgBadger: A fast PostgreSQL log analyzer 2.PgCluu: PostgreSQL and system performances monitoring and auditing tool 3.Powa: PostgreSQL Workload Analyzer. Gathers performance stats and provides real-time charts and graphs to help monitor and tune your PostgreSQL servers. Similar to Oracle AWR. 4.PgObserver: monitor performance metrics of different PostgreSQL clusters. 5.OPM: Open PostgreSQL Monitoring. Gather stats, display dashboards and send warnings when something goes wrong. Tend to be similar to Oracle Grid Control. 6.check_postgres: script for monitoring various attributes of your database. It is designed to work with Nagios, MRTG, or in standalone scripts. 7.Pgwatch: monitor PostgreSQL databases and provides a fast and efficient overview of what is really going on. 8.pgAgent:pgAgent is a job scheduler for PostgreSQL which may be managed using pgAdmin. Prior to pgAdmin v1.9, pgAgent shipped as part of pgAdmin. From pgAdmin v1.9 onwards, pgAgent is shipped as a separate application. 9.pgbouncer:PgBouncer is a lightweight connection pooler for PostgreSQL.It contains the connection pooler and it is used to establish connection between application and database
  • 13. Table Info select table_catalog, table_schema, table_name, table_type from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema'); select * from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema') and table_schema not like 'pg_toast%'; Table Activity SELECT * FROM pg_stat_all_tables; Table Block Activity SELECT * FROM pg_statio_all_tables; How to find the largest table in the postgreSQL database? SELECT relname, relpages FROM pg_class ORDER BY relpages DESC; SELECT relname, relpages FROM pg_class ORDER BY relpages DESC limit 1; Object List select nsp.nspname as object_schema, cls.relname as object_name, rol.rolname as owner, case cls.relkind when 'r' then 'TABLE' when 'm' then 'MATERIALIZED_VIEW' when 'i' then 'INDEX' when 'S' then 'SEQUENCE' when 'v' then 'VIEW' when 'c' then 'TYPE' else cls.relkind::text end as object_type from pg_class cls join pg_roles rol on rol.oid = cls.relowner join pg_namespace nsp on nsp.oid = cls.relnamespace where nsp.nspname not in ('information_schema', 'pg_catalog’) and nsp.nspname not like 'pg_toast%’ order by nsp.nspname, cls.relname; --and rol.rolname = current_user --- remove this if you want to see all objects Current User objects SELECT * FROM pg_tables t WHERE t.tableowner = current_user; SELECT relname as "Table", pg_size_pretty(pg_total_relation_size(relid)) As "Size", pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "External Size“ FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC; Size of the Objects SELECT relname AS objectname, relkind AS objecttype, reltuples AS "#entries", pg_size_pretty(relpages::bigint*8*1024) AS size FROM pg_class WHERE relpages >= 8 ORDER BY relpages DESC;
  • 14. Handy queries List procedure/function SELECT * FROM pg_proc WHERE proname='__procedurename__'; List view (including the definition) SELECT * FROM pg_views WHERE viewname='__viewname__'; Show DB table space in use SELECT pg_size_pretty(pg_total_relation_size('__table_name__')); Show DB space in use SELECT pg_size_pretty(pg_database_size('__database_name__')); SELECT pg_database_size(current_database()); SELECT pg_database_size('postgres'); select pg_size_pretty(pg_database_size(current_database())); Show current user's statement timeout show statement_timeout; Show table indexes SELECT * FROM pg_indexes WHERE tablename ='__table_name__' AND schemaname='__schema_name__'; Get all indexes from all tables of a schema: SELECT t.relname AS table_name, i.relname AS index_name, a.attname AS column_name FROM pg_class t, pg_class i, pg_index ix, pg_attribute a, pg_namespace n WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)AND t.relnamespace = n.oid AND n.nspname = 'kartones' ORDER BY t.relname, i.relname;
  • 15. •Execution data: o Queries being executed at a certain DB: SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query FROM pg_stat_activity WHERE datname='__database_name__'; •Get all queries from all dbs waiting for data (might be hung): SELECT * FROM pg_stat_activity WHERE waiting='t' •Currently running queries with process pid: SELECT pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_activity(s.backendid) AS current_query FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s; Casting: •CAST (column AS type) or column::type •'__table_name__'::regclass::oid: Get oid having a table name Query analysis: •EXPLAIN __query__: see the query plan for the given query •EXPLAIN ANALYZE __query__: see and execute the query plan for the given query •ANALYZE [__table__]: collect statistics Keyboard shortcuts • CTRL + R: reverse-i-search Tools •ptop and pg_top: top for PG. Available on the APT repository from apt.postgresql.org. •pg_activity: Command line tool for PostgreSQL server activity monitoring. •Unix-like reverse search in psql: $ echo "bind "^R" em-inc-search-prev" > $HOME/.editrc $ source $HOME/.editrc
  • 16. Find Out Essential Settings in a Postgres DB show config_file; show effective_cache_size; show shared_buffers; show all; How to calculate postgreSQL database size in disk ? SELECT pg_database_size('postgres'); SELECT pg_size_pretty(pg_database_size('postgres')); How to calculate postgreSQL table size in disk ? SELECT pg_size_pretty(pg_total_relation_size('k1')); SELECT pg_size_pretty(pg_relation_size('k1')); select pg_relation_size('audit_hist'); How to view the indexes of an existing postgreSQL table ? Syntax: # d k1 d pg_attribute
  • 17. Useful command : # l : to list all databases # ? : to list help for all commands # h create table : show help with topics # d : to list all tables # df : to list all functions # da : to list all aggregate functions # dn : to list all schemas # du : to list all users/privileges # r : to reset the query buffer # db : to list all table space dt – table list dv views d+ students – table description dy: List events di: List indexes dT+: List all data types
  • 18. PostgreSQL Scripts Table Count SELECT table_schema as Schema_Name ,count(*) as Table_Count FROM information_schema.tables WHERE table_schema IN ('testdb') and table_type='BASE TABLE' group by table_schema Table List & Row count SELECT nspname AS schemaname,relname as Tablename ,reltuples as numRows FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname IN (‘testdb') AND relkind='r’ ORDER BY relname; Column list select table_schema, table_name, ordinal_position as position, column_name, data_type, case when character_maximum_length is not null then character_maximum_length else numeric_precision end as max_length, is_nullable, column_default as default_value from information_schema.columns where table_schema in ('testdb') order by table_schema, table_name, ordinal_position; Constraint list select tc.constraint_schema as Owner, tc.table_name as TableName, kcu.column_name as ColumnName, tc.constraint_name, tc.constraint_type from information_schema.table_constraints as tc join information_schema.key_column_usage as kcu on (tc.constraint_name = kcu.constraint_name and tc.table_name = kcu.table_name) join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name where tc.constraint_schema = 'testdb' --- No search CONDITION in Postgresql Indexes list SELECT indexname,tablename FROM pg_indexes WHERE schemaname = 'testdb' ORDER BY tablename, indexname; Partition list (Table Partition) select c.relnamespace::regnamespace::text as schema, c.relname as table_name, pg_get_partkeydef(c.oid) as partition_key from pg_class c where c.relkind = 'p' and c.relnamespace::regnamespace::text='testdb'; Function & Procedure List SELECT routine_name as objects FROM information_schema.routines WHERE routine_type='FUNCTION' AND specific_schema IN ('testdb') and routine_name not like '%$%' Triggers list select trigger_schema, trigger_name from information_schema.triggers where trigger_schema='testdb' order by trigger_name; Views list select table_schema as schema_name, table_name as view_name from information_schema.views where table_schema in ('testdb') order by schema_name, view_name; User Lists select s.nspname as table_schema, s.oid as schema_id, u.usename as owner from pg_catalog.pg_namespace s join pg_catalog.pg_user u on u.usesysid = s.nspowner where nspname IN (testdb') order by table_schema;
  • 19. lists all materialized views select schemaname as schema_name, matviewname as view_name, matviewowner as owner, ispopulated as is_populated, definition from pg_matviews where schemaname='testdb_schema'order by schema_name, view_name; lists tables with primary key columns select kcu.table_schema, kcu.table_name, tco.constraint_name, kcu.ordinal_position as position, kcu.column_name as key_column from information_schema.table_constraints tco join information_schema.key_column_usage kcu on kcu.constraint_name = tco.constraint_name and kcu.constraint_schema = tco.constraint_schema and kcu.constraint_name = tco.constraint_name where tco.constraint_type = 'PRIMARY KEY' and kcu.table_schema='testdb_schema' order by kcu.table_schema, kcu.table_name, position; Sequence SELECT sequence_schema,sequence_name FROM information_schema.sequences where sequence_schema=‘testdb'; Foreign Constraints SELECT tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_schema AS foreign_table_schema, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema WHERE tc.constraint_type = 'FOREIGN KEY' and tc.table_schema=‘testdb' DB LINK SELECT PRONAME,PRONAMESPACE,PROOWNER FROM pg_catalog.pg_proc WHERE prosrc ILIKE '%dblink%';