PostgreSQL Other Scripts
PostgreSQL Other Scripts
Databases, Schemas, Tables, Roles, Permissions, Extensions, Sessions, Sequences & DBA Utilities
Introduction
PostgreSQL database administration involves many day-to-day activities outside backup, restore, monitoring, and performance troubleshooting. DBAs regularly need to inspect databases, schemas, tables, roles, privileges, extensions, sequences, configuration settings, sessions, and object ownership.
This page provides a practical collection of PostgreSQL utility scripts that can be used during administration, troubleshooting, migration preparation, security reviews, environment assessments, and routine DBA activities.
Contents
1. List All Databases 2. List Schemas 3. List User Tables 4. List Table Columns 5. List Roles and Users 6. Create a Read-Only User 7. Check Table Privileges 8. Check Object Ownership 9. List Installed Extensions 10. List Sequences 11. Cancel or Terminate a Session 12. Review PostgreSQL Configuration 13. Locate PostgreSQL Configuration Files 14. Database Ownership and Connection Settings 15. Database Activity Summary1. List All Databases
This query lists PostgreSQL databases together with their owners, encoding, connection permissions, and database size.
2. List Schemas
3. List User Tables
Use this script to list user tables and their owners across application schemas.
4. List Table Columns
The information schema provides a portable way to inspect column definitions.
5. List Roles and Users
PostgreSQL manages database users through roles. This query shows important role attributes without exposing password hashes.
6. Create a Read-Only User
The following example creates a login and grants read-only access to existing
tables in the public schema.
Grant Read Access to Future Tables
Default privileges are defined for objects created in the future by the role
executing the ALTER DEFAULT PRIVILEGES command. If application
objects are created by a different owner, specify that owner explicitly.
7. Check Table Privileges
8. Check Object Ownership
Ownership is important because PostgreSQL object owners have significant control over their objects. Unexpected ownership is particularly important after migrations or restores.
9. List Installed Extensions
Installed extensions are an important dependency to capture before migrations, upgrades, restores, or environment rebuilds.
Check Available Extension Versions
10. List Sequences
Sequence metadata is useful during migration validation, identity troubleshooting, and database inventory activities.
11. Cancel or Terminate a Session
When intervention is necessary, consider cancelling the currently running statement before terminating the entire database session.
Cancel the Current Query
Terminate the Session
12. Review PostgreSQL Configuration
The pg_settings view provides the effective PostgreSQL configuration
together with useful metadata such as source and restart requirements.
Show Settings Pending Restart
13. Locate PostgreSQL Configuration Files
These settings help DBAs locate the active configuration files and PostgreSQL data directory.
postgresql.conf or pg_hba.conf should follow
the appropriate change-control and validation process.
14. Database Ownership and Connection Settings
15. Database Activity Summary
This query provides a convenient overview of activity statistics across the PostgreSQL databases on the server.
PostgreSQL DBA Utility Reference
| Requirement | Primary PostgreSQL Source | Typical Use |
|---|---|---|
| Database inventory | pg_database |
Database owner, encoding, availability, and limits |
| Schema inventory | pg_namespace |
Schemas and ownership |
| Table inventory | pg_tables |
Tables, owners, and tablespaces |
| Roles | pg_roles |
Login and role attributes |
| Extensions | pg_extension |
Installed extension inventory |
| Sequences | pg_sequences |
Sequence configuration and value checks |
| Configuration | pg_settings |
Effective configuration and restart requirements |
| Activity | pg_stat_database |
Database-level activity statistics |
Production DBA Utility Checklist
- Maintain an inventory of PostgreSQL databases and owners.
- Document application schemas and object ownership.
- Review privileged roles regularly.
- Use least-privilege access for application and reporting accounts.
- Review table and schema privileges after migrations and restores.
- Document installed PostgreSQL extensions and versions.
- Validate sequence state after data migrations.
- Review configuration changes and pending restart requirements.
- Protect PostgreSQL configuration and authentication files.
- Identify session ownership before cancelling or terminating activity.
- Record production administrative changes through the approved change process.
- Maintain tested rollback procedures for security and configuration changes.
Key Takeaways
- PostgreSQL system catalogs provide detailed database, object, role, and ownership information.
- Roles should follow least-privilege principles.
- Default privileges require careful attention to the role that creates future objects.
- Extensions should be documented because they can become important migration and recovery dependencies.
- Sequence state should be validated after data migrations and manual data loads.
- Cancelling a query is different from terminating the entire database session.
- Session termination can roll back application transactions and should be performed carefully.
pg_settingsprovides the effective configuration and indicates settings requiring restart.- Configuration and authentication files should be treated as security-sensitive assets.
- Administrative utility scripts should be tested before use in production environments.
Conclusion
Day-to-day PostgreSQL administration requires reliable ways to inspect database objects, security, ownership, configuration, extensions, sequences, and session activity. These utility scripts provide a practical starting point for common DBA operations without requiring separate scripts for every routine investigation.
The most important principle is to distinguish informational queries from commands that modify the environment. Queries that inspect catalogs and statistics are generally low risk, while role changes, privilege modifications, ownership changes, configuration updates, and backend termination require greater care.
For production environments, administrative actions should be documented, tested where appropriate, performed through approved change procedures, and validated after implementation.
Comments
Post a Comment