The document discusses PostgreSQL database administration. It describes how users connect and authenticate to the database using methods defined in pg_hba.conf. It lists some common PostgreSQL configuration files and authentication methods. It also covers the roles of database administration like defining users, roles, and managing access privileges.
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.
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%';