pg_read_server_files#
What is pg_read_server_files?#
pg_read_server_files is a built-in role that allows reading files from the PostgreSQL serverโs filesystem.
- In simple words:
โI can read files that exist on the database server machine.โ
โ ๏ธ This is NOT about reading table data
โ ๏ธ This is NOT about reading client/local files
โ ๏ธ This is server-side filesystem access
What this role ALLOWS#
A role with pg_read_server_files can use functions like:
๐Read files
pg_read_file()
pg_read_binary_file()
Example:
SELECT pg_read_file('/etc/hostname');
๐ List directories
pg_ls_dir()
Example:
SELECT pg_ls_dir('pg_log');
๐Read log files
SELECT pg_read_file('log/postgresql-2024-01-01.log');
- Typical readable targets
PostgreSQL log files
Config files (sometimes)
Data directory metadata
OS text files (if permissions allow)
โ What it does NOT allow
Action |
Allowed |
|---|---|
Read table data |
โ |
Modify files |
โ |
Write files |
โ |
Execute OS commands |
โ |
Access client filesystem |
โ |
Writing files requires pg_write_server_files
Executing programs requires pg_execute_server_program
Why this role is DANGEROUS
Even though itโs โread-onlyโ, it can expose:
- Sensitive data risks
SSL private keys
Password files
Config secrets
OS user info
Backup locations
Example:
SELECT pg_read_file('/var/lib/postgresql/data/postgresql.conf');
- That may reveal:
Ports
Users
Paths
Extensions
Logging destinations
Mental model
pg_read_server_files = โRead access to the database serverโs hard driveโ
Not your laptop
Not the client
The actual DB server machine
- Common legitimate use cases
โ DBAs reading logs from SQL
โ Debugging startup issues
โ Monitoring tools
โ Controlled maintenance scripts
- Bad use cases (DO NOT)
โ Application users
โ Web apps
โ Shared reporting users
โ Multi-tenant SaaS users
Relationship with other roles#
Role |
Capability |
|---|---|
pg_read_server_files |
๐ Read files |
pg_write_server_files |
โ๏ธ Write files |
pg_execute_server_program |
Run OS commands |
pg_monitor |
Stats only (safe) |
Never combine these casually
- Best practices (VERY IMPORTANT)
โ Grant only to trusted DBAs
โ Use temporarily, then revoke
โ Audit usage
โ Never grant to app roles
โ Never grant in cloud/shared DBs
Cloud note (AWS / GCP / Azure)
- Cloud providers often:
Restrict file paths
Block access to sensitive OS files
Log usage heavily
Still dangerous if misused.
Example grant
GRANT pg_read_server_files TO dba_user;
To revoke:
REVOKE pg_read_server_files FROM dba_user;
Summary
Feature |
Value |
|---|---|
Access level |
๐ด High |
Reads server files |
โ |
Writes server files |
โ |
Executes OS commands |
โ |
Safe for apps |
โ |