{"id":372,"date":"2020-09-12T12:09:52","date_gmt":"2020-09-12T06:39:52","guid":{"rendered":"https:\/\/www.bhuvankrishna.in\/blog\/?p=372"},"modified":"2020-09-12T12:17:58","modified_gmt":"2020-09-12T06:47:58","slug":"extract-data-from-postgres-in-docker-and-save-it-to-csv","status":"publish","type":"post","link":"https:\/\/www.bhuvankrishna.in\/blog\/2020\/09\/12\/extract-data-from-postgres-in-docker-and-save-it-to-csv\/","title":{"rendered":"Extract data from postgres in docker and save it to csv"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The case is simple postgres database is hosted in a docker image and to access the database use this command. docker image name can be found with this command <code>docker ps -a<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>docker exec -it &lt;docker-image&gt; psql -U postgres<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>docker exec -it &lt;docker-image> psql -U postgres -a &lt;dbname> -c \"copy(select u.name user_name,p.name place_name, attempts,p.created from<br>place p inner join users u on u.id=p.user_id order by attempts desc) to stdout with csv header\" > data-list.csv<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Breaking down the command <code>u.name user_name<\/code> is the field in user table with alias of user_name. <code>p.name place_name<\/code> is the field in place table with alias of place_name, r.created is the field in place table. <code>place p<\/code> is naming place table with &#8216;p&#8217; and inner join joins the user id in both the tables. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The case is simple postgres database is hosted in a docker image and to access the database use this command. docker image name can be found with this command docker ps -a. docker exec -it &lt;docker-image&gt; psql -U postgres docker exec -it &lt;docker-image> psql -U postgres -a &lt;dbname> -c &#8220;copy(select u.name user_name,p.name place_name, attempts,p.created fromplace\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.bhuvankrishna.in\/blog\/2020\/09\/12\/extract-data-from-postgres-in-docker-and-save-it-to-csv\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-372","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":4,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":376,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/372\/revisions\/376"}],"wp:attachment":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}