
Version
Retrieve the PostgreSQL version using the command:psql -V
List of Databases
Get a list of databases using:psql -l
Connecting to the “postgres” Database
Connect to the “postgres” database with:psql -d postgres
Gathering Information
Execute the following commands within the PostgreSQL console (after connecting with psql) to gather system information:
| |
These commands provide details about users, roles, settings, and the configuration files, namely the main PostgreSQL configuration file (postgresql.conf) and the client authentication configuration file (pg_hba.conf).
A. Client Authentication
- Disable Unsecured Access: Instead of relying on trust connections, enforce robust authentication methods like MD5 or Kerberos. Trust-based authentication over TCP/IP should be avoided unless every user on every connecting machine is implicitly trusted. It’s generally only acceptable for connections from the local machine (127.0.0.1).
- Auditing Trust Configurations: When auditing, verify that there are no instances of the “trust” method defined in your
pg_hba.conffile. - Restrict IP Ranges: Configure your server to only accept connections from authorized IP addresses.
- Enforce SSL Connections:
- Enable SSL at the server level by setting “ssl” to “on” in your
postgresql.conffile. - Use “hostssl” entries in
pg_hba.confto enforce SSL for specific connections. - Ensure that SSL certificate files (
ssl_ca_fileandssl_cert_file) are properly configured in your PostgreSQL settings. You can check these usingpg_settings.
- Enable SSL at the server level by setting “ssl” to “on” in your
B. User Roles and Access Privileges
- Limit Powerful Roles: Restrict roles with the attributes “Superuser”, “Create role”, and “Create DB” to database administrators (DBAs) only.
- Auditing Role Privileges: Regularly review role attributes. You can list roles and their attributes within the PostgreSQL console using:
1postgres-> \du - Implement Role Expiration: Set expiration dates for roles using the
VALID UNTILclause. - Auditing Expiration Dates: To check if expiration dates are set, examine the “rolvaliduntil” column in the
pg_rolestable by running:1postgres=> select * from pg_roles;
C. Connection and Login Restrictions
- Password Security:
- Enable password encryption by setting
password_encryptionto “on” in your configuration. - Verify password storage by querying the
pg_shadowtable:1SELECT * FROM pg_shadow; - Check for the availability of the ‘pgcrypto’ extension to ensure strong encryption capabilities:
1SELECT * FROM pg_available_extensions where name='pgcrypto';
- Enable password encryption by setting
- Session Management: Within your
postgresql.conffile, configure the following settings to manage session timeouts and keepalives:statement_timeout = 10000(in milliseconds)tcp_keepalives_idle = 10(in seconds)tcp_keepalives_interval = 10(in seconds)tcp_keepalives_count = 10
D. Logging
- Enable and Configure Logging: In your
postgresql.conffile:- Set
log_statementto an appropriate level other than “none.” - Set
log_file_modeto0600for secure log file permissions. - Define the log output directory using
log_destination. - Enable connection logging with
log_connections = on. - Customize the log line prefix if needed using
log_line_prefix.
- Set
- Additional Logging Settings: Use
pg_settingsto confirm the following settings are enabled:log_connectionsshould be set toon.log_disconnectionsshould be set toon.
- Check for Auditing Extensions: List shared libraries using
SHOW shared_preload_librariesto see if auditing extensions likepgauditare installed and active.
E. Unnecessary Database Components
- Remove Multiple PostgreSQL Versions: Use the following command to find and remove any redundant PostgreSQL installations:
1rpm -qa | grep postgres - Manage Extensions: Review and uninstall any unnecessary extensions:
- List non-core extensions:
1postgres=> select * from pg_extension where extname != 'plpgsql'; - Show installed extensions:
1postgres=> SELECT * FROM pg_available_extensions WHERE installed_version IS NOT NULL;
- List non-core extensions:
- Review File System Permissions: Regularly inspect and properly set permissions for database files, log files, and backup files using commands like:
1ls -lR
References