How to export table data to file / csv – PostgreSQL

PostgreSQL comes with an easy to use export tool to export data from PostgreSQL database. In this tutorial, we show you how to export data from PostgreSQL to a file or csv file.

1. Connect PostgreSQL

Use psql command to connect to the PostgreSQL database.


$ psql -p 5433 -U dba dbname

P.S 5433 is my PostgreSQL port number.

2. Export Ready

Type “\o /home/yongmo/data25000.csv“, it tell PostgreSQL that next query result will be exported to file “/home/yongmo/data25000.csv”.


dbname=> \o /home/yongmo/data25000.csv

3. Query to Export

Now, issue a normal query.


dbname=> select url from urltable where scoreid=1 limit 25000;

The entire query’s result will be exported to /home/yongmo/data25000.csv.

4. Full Example

Here is the full command.


yongmo@abcdb:~$ psql -p 5433 -U dba dbname
Password for user dba: 
Welcome to psql 8.2.4 (server 8.3.3), the PostgreSQL interactive terminal.
 
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
 
WARNING:  You are connected to a server with major version 8.3,
but your psql client is major version 8.2.  Some backslash commands,
such as \d, might not work properly.
 
dbname=> \o /home/yongmo/data25000.csv
dbname=> select url from urltable where scoreid=1 limit 25000;
dbname=> \q

27 comments on “How to export table data to file / csv – PostgreSQL

  1. This does not create an CSV file, result is like this:

     iddes | langid |                label                |  id  |  north   |  south   |   west   |   east
         1 | de     | Armenien                            |    1 |  41,2971 |  38,8411 |  43,4542 |  46,6205
         2 | de     | Afghanistan                         |    2 |   38,472 |  29,4061 |  60,5042 |  74,9157
         3 | de     | Albanien                            |    3 |  42,6603 |   39,645 |  19,2885 |  21,0533
         4 | de     | Algerien                            |    4 |  37,0899 |  18,9764 | -8,66722 |  11,9865
         5 | de     | Amerikanisch Samoa                  |    5 | -14,2543 | -14,3756 | -170,823 | -170,562
         6 | de     | Andorra                             |    6 |   42,656 |  42,4364 |  1,42139 |  1,78172
    [...]
    
  2. Oops looks like someone else already suggested copy.

    I wish they had mentioned that it would handle the escaping cleanly.

  3. OK. I finally figured out the right way to do this.

    Basically you need to use the COPY command. I.e.

    COPY (SQL_QUERY) TO FILE_NAME WITH CSV HEADER

    e.g.

    psql -p 5433 -U dba dbname -c “COPY (select url from urltable where scoreid=1 limit 25000) TO STDOUT WITH CSV HEADER;” > /home/yongmo/data25000.csv

    I’m glad this there’s a way to do this but I wish it was better documented.

  4. PLEASE DON’T TRY THIS.

    THIS DOESN’T DO ANY ESCAPING.

    If your data contains comments in field data your files will break. Data with embedded new lines will be even worst.

    Postgresql needs a real export to CSV for select statements. Unfortunately in does have this functionality.

    1. Hi , thanks for your comment, i didn’t know about it. Ya it may not work in your case (escaping) , however it work fine in most of the case, usually data wont care about escape right?

    2. You can handle the line breaks thusly, if url has line breaks in the results:

      select ‘”‘||url||'”‘ from urltable where scoreid=1 limit 25000;

      That will enclose the url results in quotation marks so the line breaks will be preserved without forcing them as newlines in the csv.

  5. Why not use \copy

    \copy (select url from urltable where scoreid=1 limit 25000) TO ‘/tmp/filename.csv’ CSV

    \copy command can do various other tricks aswell.

  6. I can not do it please advise me more i am very poorly

    dbname=>\o /W2k3fs3/shared/Transfer/data25000.csv
    /W2k3fs3/shared/Transfer/data25000.csv: No such file or directory

          1. Thank.

            if i want to save in my computer in /c:/no file can you rewrite command for me please

            Many Thank.

Leave a Comment

Your email address will not be published. Required fields are marked *