{"id":9736,"date":"2025-09-05T08:05:35","date_gmt":"2025-09-05T08:05:35","guid":{"rendered":"http:\/\/www.withdata.com\/blog\/?p=9736"},"modified":"2025-09-05T09:05:34","modified_gmt":"2025-09-05T09:05:34","slug":"export-data-from-postgresql-to-csv-via-command-line-on-linux","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/pg-cmd\/export-data-from-postgresql-to-csv-via-command-line-on-linux.html","title":{"rendered":"Export data from PostgreSQL to CSV via command line on Linux"},"content":{"rendered":"<p>Want to export  <a href=\"https:\/\/www.postgresql.org\/\">PostgreSQL<\/a>  data to  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\">CSV<\/a>  file via Command Line Interface (CLI) on Linux (Ubuntu\/RedHat\/CentOS&#8230;)?<\/p>\n<p>Using <a href=\"https:\/\/www.withdata.com\/pg-cmd\/\">pg-cmd<\/a>, Command Line Interface (CLI) client for PostgreSQL, you can unload PostgreSQL  data to CSV\/TSV\/TXT easily and fast. Direct Interaction or Scripting and Automation.<\/p>\n<ul>\n<li>No need to install PostgreSQL client library.<\/li>\n<li>Convenient to deploy to cloud. No need to run installation file, just uncompress and run, immediately.<\/li>\n<li>Commands are entered into a terminal or command prompt, accessed from local or remote (like SSH).<\/li>\n<li>Support Windows, Linux, and MacOS.<\/li>\n<\/ul>\n<p><a href=\"#download-top\" onclick=\"dtv.style.display = 'block';\">Download pg-cmd Linux version<\/a>  <a href=\"#download-top\" onclick=\"dtv.style.display = 'block';\"><img decoding=\"async\" src=\"https:\/\/www.withdata.com\/images\/download_3.jpg\" alt=\"Download pg-cmd\" width=\"80\" height=\"22\" style=\"max-width:100%;height:auto;\" \/><\/a> <span id=\"dtv\" style=\"display: none;\"><br \/>\n<a href=\"\/down\/pg-cmd.linux-386.zip\">pg-cmd.linux-386.zip<\/a><br \/>\n<a href=\"\/down\/pg-cmd.linux-amd64.zip\">pg-cmd.linux-amd64.zip<\/a><br \/>\n<a href=\"\/down\/pg-cmd.linux-arm64.zip\">pg-cmd.linux-arm64.zip<\/a><\/p>\n<p><\/span><\/p>\n<h2>Export PostgreSQL  data to CSV\/TSV\/TXT via command line on Linux (Ubuntu\/RedHat\/CentOS&#8230;)<\/h2>\n<p><b>1. Unload Table.<\/b><\/p>\n<p>1) In interactive mode:<\/p>\n<pre>\npg-cmd> unload\n      > table=media_messages \n      > datafile=\/data\/media_messages.csv \n      > delimiter=, \n      > quote=\" \n      > header=y;\n<\/pre>\n<p>2) In batch mode:<\/p>\n<pre>\npg-cmd dsn=\"pan:withdata@192.168.0.102\/test\" task=unload table=media_messages datafile=\/data\/media_messages.csv delimiter=, quote=\" header=y quit=y\n<\/pre>\n<p><b>2. Unload Query.<\/b><\/p>\n<p>1) In interactive mode:<\/p>\n<pre>\npg-cmd> unload\n      > query=\"select * from media_messages where id > 100\"\n      > datafile=\/data\/media_messages.csv \n      > delimiter=, \n      > quote=\" \n      > header=y;\n<\/pre>\n<p>2) In batch mode:<\/p>\n<pre>\npg-cmd dsn=\"pan:withdata@192.168.0.102\/test\" task=unload query=\"select * from media_messages where id > 100\" datafile=\/data\/media_messages.csv delimiter=, quote=\" header=y quit=y\n<\/pre>\n<p><b>3. Unload All Tables.<\/b><\/p>\n<p>1) In interactive mode:<\/p>\n<pre>\npg-cmd> unload\n      > table=all \n      > folder=\/data\/tables_data\n      > delimiter=, \n      > quote=\" \n      > header=y;\n<\/pre>\n<p>2) In batch mode:<\/p>\n<pre>\npg-cmd dsn=\"pan:withdata@192.168.0.102\/test\" task=unload table=all folder=\/data\/tables_data delimiter=, quote=\" header=y quit=y\n<\/pre>\n<p>See <a href=\"https:\/\/www.withdata.com\/pg-cmd\/unload.html\">parameters detail<\/a>.<\/p>\n<hr style=\"clear: both;\" \/>\n<p>More about <a href=\"https:\/\/www.withdata.com\/pg-cmd\/\">pg-cmd &#8211; Command Line Interface (CLI) tool for PostgreSQL<\/a><\/p>\n<div id=\"download-bottom\" style=\"clear: both;\">\n<p>Download pg-cmd Linux version<a href=\"#download-bottom\" onclick=\"dbv.style.display = 'block';\"><img decoding=\"async\" src=\"https:\/\/www.withdata.com\/images\/btn_download_green.png\" alt=\"Download pg-cmd\" width=\"165\" height=\"50\" style=\"max-width:100%;height:auto;\" \/><\/a> <span id=\"dbv\" style=\"display: none;\"><br \/>\n<a href=\"\/down\/pg-cmd.linux-386.zip\">pg-cmd.linux-386.zip<\/a><br \/>\n<a href=\"\/down\/pg-cmd.linux-amd64.zip\">pg-cmd.linux-amd64.zip<\/a><br \/>\n<a href=\"\/down\/pg-cmd.linux-arm64.zip\">pg-cmd.linux-arm64.zip<\/a><\/p>\n<p><\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Want to export PostgreSQL data to CSV file via Command Line Interface (CLI) on Linux (Ubuntu\/RedHat\/CentOS&#8230;)? Using pg-cmd, Command Line Interface (CLI) client for PostgreSQL, you can unload PostgreSQL data to CSV\/TSV\/TXT easily and fast. Direct Interaction or Scripting and Automation. No need to install PostgreSQL client library. Convenient to deploy to cloud. No need [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2555],"tags":[73,34,16,2553,1370,1219],"class_list":["post-9736","post","type-post","status-publish","format-standard","hentry","category-pg-cmd","tag-command-line","tag-csv","tag-export","tag-linux","tag-postgresql","tag-postgresql-export-csv"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/9736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=9736"}],"version-history":[{"count":5,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/9736\/revisions"}],"predecessor-version":[{"id":40061,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/9736\/revisions\/40061"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=9736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=9736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=9736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}