Exporting data from a PostgreSQL database
One of the functionalities of the COPY command is to retrieve data from your database and dump it into the file location and format that you choose. For example, consider the following statement:
COPY (SELECT * FROM products LIMIT 5)
TO STDOUT
WITH CSV HEADER;
The following is the output of the code:
product_id,model,year,product_type,base_msrp,production_start_date,production_end_date
1,Lemon,2016,scooter,399.99,2015-10-28 00:00:00,2018-02-03 00:00:00
2,Lemon Limited Edition,2017,scooter,799.99,2016-08-30 00:00:00,2016-11-24 00:00:00
3,Lemon,2019,scooter,499.99,2018-12-27 00:00:00,2024-08-24 00:00:00
5,Blade,2020,scooter,699.99,2020-02-17 00:00:00,2020-09-23 00:00:00
7,Bat,2022,scooter,599.99,2022-06-07 00:00:00,
Note that COPY is a psql command. You need to run it inside the psql prompt. This command returns five rows from the products table, with each record on a new line, and each value separated by a comma, in a typical...