Sub topic 8
Privileges and security for server-side file access
Assume you know basic SQL, PostgreSQL data types, CSV files, and the psql command-line client. This chapter focuses on the permissions and security boundaries involved when COPY reads or writes a file on the database server.
Two machines, two permission checks
When you use a filename with SQL COPY, PostgreSQL accesses the file from the database server's filesystem. The file is read or written by the operating-system account running PostgreSQL, often named postgres. Your shell user's file permissions do not control this operation.
The database checks two kinds of permission:
- Database privileges on the table or columns.
- A special PostgreSQL role that permits server-side file access.
For example, importing requires permission to insert into the target table and permission to read server files:
GRANT INSERT ON products TO loader_role;
GRANT pg_read_server_files TO loader_role;Exporting requires permission to select from the source table and permission to write server files:
GRANT SELECT ON products TO reporter_role;
GRANT pg_write_server_files TO reporter_role;These predefined roles are powerful. A role with pg_read_server_files can read files that the PostgreSQL operating-system account can access, not only files belonging to one application. A role with pg_write_server_files can create or replace files accessible to that operating-system account.
| Operation | Table privilege | Server file role |
|---|---|---|
| `COPY products FROM '/path/file.csv'` | `INSERT` | `pg_read_server_files` |
| `COPY products TO '/path/file.csv'` | `SELECT` | `pg_write_server_files` |
| `COPY ... PROGRAM 'command'` | Depends on the operation | `pg_execute_server_program` |
The exact filesystem access still depends on the operating-system account, directory permissions, ownership, and security controls such as SELinux or AppArmor.
Server-side `COPY` and client-side `\copy`
The psql command \copy moves the file access to the client machine. psql opens the local file and sends its contents to PostgreSQL through the connection. The database server receives a stream, so it does not need permission to open the local path.
\copy products (id, name, price, released, notes) \
FROM 'products.csv' \
WITH (FORMAT csv, HEADER true)In this example, products.csv must be readable by the user running psql. The database role still needs table privileges, such as INSERT, but it normally does not need pg_read_server_files.
For an export, \copy writes the output on the client:
\copy (SELECT id, name, price FROM products ORDER BY id) \
TO 'products_export.csv' \
WITH (FORMAT csv, HEADER true)The client user needs permission to create or replace products_export.csv. The database role needs the appropriate SELECT privilege.
A practical rule is to prefer \copy when an operator or application already has the file locally. Use server-side COPY only when the database server must access a controlled directory.
Restrict paths and avoid `PROGRAM`
Do not grant server-file roles to a broad application role merely to make an import work. Create a dedicated loader role, grant only the required table privileges, and restrict the PostgreSQL operating-system account to a dedicated import directory.
Use absolute paths so that the intended location is clear:
COPY products
FROM '/srv/postgresql-import/products.csv'
WITH (FORMAT csv, HEADER true);The directory must be readable by the PostgreSQL operating-system account. A database administrator may need to create the directory and set its ownership outside PostgreSQL.
COPY ... PROGRAM is more dangerous than a filename. It runs a command on the database server through the operating system. The command can read data, alter files, or affect other services if the operating-system account has those permissions. PostgreSQL protects this operation with the pg_execute_server_program role. Grant it only for a narrowly controlled administrative task, and never place untrusted input directly into the command string.
> callout warning > A server-side COPY path is interpreted on the database server, not on the computer where you type SQL. If the file is missing, check the server's path and the PostgreSQL operating-system account's permissions.
A safer loading pattern
For routine uploads, let a controlled process place files in a known directory, then use \copy from that process or grant a narrowly scoped server-file role to a dedicated loader. Load into a staging table when validation or transformation is required. This limits the privileges needed on the production table and makes rejected data easier to inspect.
Exercise
An analyst runs \copy products FROM 'products.csv' WITH (FORMAT csv, HEADER true) from a laptop. The command fails because the analyst cannot read products.csv. Which machine's permissions should be checked first, and which server-file role is usually unnecessary?
Show answer
Check the laptop user's permissions on products.csv. Because \copy reads the client file, pg_read_server_files is usually unnecessary; the database role still needs INSERT on products.