

Nice! But that doesn’t quite look like CSV. Seems like the command copied all four rows. Try to run the above command again, this time with \copy 2: db=# \copy users TO 'users.csv' To achieve this, \copy uses COPY TO STDOUT under the hood (and COPY FROM

Those of the local user, not the server, and no SQL superuser privileges are This means that file accessibility and privileges are This is an operation that runs an SQLĬOPY command, but instead of the server reading or writing the specifiedįile, psql reads or writes the file and routes the data between the server and To remedy this, psql provides a meta-command called \copy that works just likeĬOPY, but without the above limitations. On my computer is obviously inaccessible by that PostgreSQL user on the When connecting to a remote database, any file User ID the server runs as) and the name must be specified from the viewpoint The file must be accessible by the PostgreSQL user (the

Nicer to work with relative paths, obviously.ĬOPY with a file name instructs the PostgreSQL server to directly read from The docįor the filename argument specifically states:Īn input file name can be an absolute or relative path, but an output fileįair enough, we can specify an absolute path if we have to. Looks like that didn’t work… Maybe the docs for COPY can help. We can now copy this entire table to a file in a psql session: db=# COPY users TO 'users.csv' ĮRROR: relative path not allowed for COPY to file
PGADMIN 4 EXPORT QUERY TO CSV SERIAL
Let’s introduce a small table that we’ll use to explore the COPY commandĪ bit: CREATE TABLE users ( id serial primary key, name text NOT NULL, age integer NOT NULL ) INSERT INTO users ( name, age ) VALUES ( 'Anne', 24 ), ( 'Bill', 37 ), ( 'Carrie', 39 ), ( 'Daniel', 19 ) The interface of the COPY TO command is as follows 1: COPY As we’re talking aboutĮxporting data, we’ll be focusing on the former use case. It is also used to move data from a file to a table. The COPY command copies data from a table or from a query to a file. After all, I’mĪ fan of using PostgreSQL on the command line. In thisĪrticle I’ll be focusing on psql and the command line in general. GUIs such as pgAdmin usually have a data export feature built in. Working with databases, be it for visualizing the data, using it as input toĪnother tool, or simply sharing it with others. Running a query and exporting the resulting data is a common requirement when
