DEV Community

Hugo Martins
Hugo Martins

Posted on • Originally published at hugomartins.io on

3

Extracting Data from PostgreSQL

PostgreSQL has an excellent module built specifically for the purpose of extracting data between a file and a table.

This can be used to extract several data in three formats:- Text- CSV- Binary

Sometimes you’d want to extract data to a folder different than the one the database is in. You’d want to extract the data to the machine your logged on instead. This can be done using \copy instead of COPY.

An example of basic usage for extracting data would be, in CSV:

\copy (SELECT * FROM foo WHERE bar IS NOT NULL) TO '/path/to/folder' HEADER CSV

Enter fullscreen mode Exit fullscreen mode

This simple snippet is incredibly useful to generate reports or extract data for analysis, simply and quickly.


Source: - https://www.postgresql.org/docs/9.5/static/sql-copy.html

Originally published at hugomartins.io on October 10, 2016.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay