Sub topic 4
Server-side `COPY` versus `psql \copy`
This chapter assumes that you know basic SQL, can use the psql command-line client, understand CSV files, and know the main PostgreSQL data types. The key distinction is where PostgreSQL reads or writes the file: the database server or the client computer running psql.
Two commands, two machines
PostgreSQL provides the SQL COPY command. In its file form, the database server opens the path itself.
COPY products FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv, HEADER true);
COPY products TO '/var/lib/postgresql/export/products.csv'
WITH (FORMAT csv, HEADER true);The path is interpreted on the server. If PostgreSQL runs on a remote host, this is not a path on your laptop.
The psql client provides \copy, which is a client-side command. psql opens the local file and sends or receives data through the database connection.
\copy products FROM 'products.csv' WITH (FORMAT csv, HEADER true)
\copy products TO 'products_export.csv' WITH (FORMAT csv, HEADER true)Here, products.csv is relative to the current directory of the computer running psql.
| Command | File is accessed by | Typical permission needed |
|---|---|---|
| `COPY ... FROM/TO 'path'` | Database server | PostgreSQL server-file privilege and operating-system access |
| `\copy ... FROM/TO 'path'` | `psql` client | Local file access and normal table privileges |
How data crosses the connection
COPY also supports STDIN and STDOUT. These forms do not name a server-side file. Instead, the client or another program supplies or receives the data stream.
The psql commands below use the connection to transfer the data while keeping the files on the client:
psql "$DATABASE_URL" -c "\copy products FROM 'products.csv' WITH (FORMAT csv, HEADER true)"
psql "$DATABASE_URL" -c "\copy products TO 'products_export.csv' WITH (FORMAT csv, HEADER true)"A useful mental model is:
With server-side COPY, the server performs both the file operation and the database operation. With \copy, psql performs the file operation, while PostgreSQL still performs the database operation.
Choosing between them
Use server-side COPY when:
- The file is already on the database server.
- The server can access the file safely.
- You control the server and its file permissions.
- You want a direct server-side operation for a controlled workflow.
Use \copy when:
- The file is on your workstation or an application host.
- You do not want to copy the file to the database server first.
- You lack permission for PostgreSQL to read arbitrary server files.
- You want an export to appear directly on the client machine.
The two forms can use the same CSV options, including FORMAT csv, HEADER, DELIMITER, QUOTE, ESCAPE, and NULL.
Permissions and security
A server-side file operation needs more than permission on the target table. PostgreSQL must be allowed to read or write server files, and the operating-system account running PostgreSQL must have access to the path. PostgreSQL restricts these operations to superusers or roles with the appropriate server-file privileges, such as pg_read_server_files or pg_write_server_files.
Granting broad server-file privileges can expose files unrelated to the database. Prefer a controlled directory with narrow operating-system permissions. Do not use a user-provided path directly in administrative SQL.
The client-side \copy command avoids those server-file privileges. However, the user running psql still needs local permission to read or create the file, and the database role still needs the normal privileges for the table or query.
A practical remote example
Suppose PostgreSQL runs on db.example.net, while products.csv is in your local directory. This command imports the local file into the remote database:
psql "host=db.example.net dbname=inventory user=loader" \
-c "\copy products (id, name, price, released, notes) FROM 'products.csv' WITH (FORMAT csv, HEADER true)"The server receives rows over the connection. It never opens your local products.csv.
For an export, \copy writes the result locally:
psql "host=db.example.net dbname=inventory user=reporter" \
-c "\copy (SELECT id, name, price FROM products ORDER BY id) TO 'products_export.csv' WITH (FORMAT csv, HEADER true)"This is often the simplest choice for developer workstations and scheduled jobs that run outside the database host.
Exercise
PostgreSQL is remote, and the CSV file is on your laptop. Which command should you normally use, and where does the file access occur?
Show answer
Use psql's \copy command. The psql client on your laptop reads or writes the file, then transfers the data over the database connection.