SlideShare a Scribd company logo
1 of 19
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

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
PostgreSQL-Consulting
 
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres OpenKevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
PostgresOpen
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
Denish Patel
 
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike SteenbergenMeet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
distributed matters
 

What's hot (20)

Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
Get to know PostgreSQL!
Get to know PostgreSQL!Get to know PostgreSQL!
Get to know PostgreSQL!
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1
 
PostgreSQL : Introduction
PostgreSQL : IntroductionPostgreSQL : Introduction
PostgreSQL : Introduction
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
Postgresql 12 streaming replication hol
Postgresql 12 streaming replication holPostgresql 12 streaming replication hol
Postgresql 12 streaming replication hol
 
PostgreSQL Replication Tutorial
PostgreSQL Replication TutorialPostgreSQL Replication Tutorial
PostgreSQL Replication Tutorial
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
 
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
 
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres OpenKevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 
Intro ProxySQL
Intro ProxySQLIntro ProxySQL
Intro ProxySQL
 
Materialize: a platform for changing data
Materialize: a platform for changing dataMaterialize: a platform for changing data
Materialize: a platform for changing data
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
 
Tuning Autovacuum in Postgresql
Tuning Autovacuum in PostgresqlTuning Autovacuum in Postgresql
Tuning Autovacuum in Postgresql
 
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike SteenbergenMeet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
 

Similar to Postgresql Database Administration- Day3

PostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, StructuredPostgreSQL, MongoDb, Express, React, Structured
PostgreSQL, MongoDb, Express, React, Structured
priya951125
 
Honey I Shrunk the Database
Honey I Shrunk the DatabaseHoney I Shrunk the Database
Honey I Shrunk the Database
Vanessa Hurst
 
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

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 

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%';