Friday, July 25, 2014

Writing output of SQL command to a text file in PostgresQL

At times it's useful to direct the output of SQL command to a text file. For that follow the following steps.
  • Got to database using psql command through console. For example if database is abc then do 
                psql -U (username) abc
  • At psql prompt do the following, if data.csv is the file where you want to pipe
             abc=#  \o data.txt
  • Run the query 
            select * from <table>;
Now go the location and you can see the text file with the data in it.

No comments:

Post a Comment