<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bunlaikun</title>
    <description>The latest articles on DEV Community by Bunlaikun (@bunlaikun).</description>
    <link>https://dev.to/bunlaikun</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F872752%2Fb7a89b77-6a3e-4bf4-b5a7-bf2ce7550268.jpeg</url>
      <title>DEV Community: Bunlaikun</title>
      <link>https://dev.to/bunlaikun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bunlaikun"/>
    <language>en</language>
    <item>
      <title>How to log query in Postgres container Docker to CSV file</title>
      <dc:creator>Bunlaikun</dc:creator>
      <pubDate>Mon, 06 Jun 2022 06:02:31 +0000</pubDate>
      <link>https://dev.to/bunlaikun/how-to-log-query-in-postgres-container-docker-to-csv-file-3l7f</link>
      <guid>https://dev.to/bunlaikun/how-to-log-query-in-postgres-container-docker-to-csv-file-3l7f</guid>
      <description>&lt;p&gt;When I work with API that affects many tables in the database, It kinda frustrating to track them one by one in my backend code. So this solution saves me a lot of time.&lt;/p&gt;

&lt;p&gt;first, you need to get into a running Postgres container &lt;/p&gt;

&lt;p&gt;use &lt;code&gt;docker ps&lt;/code&gt; to find your docker name&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it NAME_OF_CONTAINER bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;now you are in root path, go in side Postgres by running this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql -U YOUR_POSTGRES_USERNAME
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;locate config file by this command then exit with &lt;code&gt;\q&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# SHOW config_file;
postgres-# \q
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I gonna use vim to edit the file. you can use nano if you like. Install by this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt-get update
apt-get install vim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after you exit from Postgres now you are in root path again go config file that you locate. mine was&lt;br&gt;
&lt;code&gt;/var/lib/postgresql/data/postgresql.conf&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /var/lib/postgresql/data/
vi postgresql.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;press &lt;code&gt;/&lt;/code&gt; in vim to find &lt;code&gt;ERROR REPORTING AND LOGGING&lt;/code&gt;&lt;br&gt;
uncomment by remove &lt;code&gt;#&lt;/code&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  in "Where to Log" section
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_destination = 'csvlog'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  in "What to Log" section
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_statement=all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;read more about Where When What to log &lt;a href="https://www.postgresql.org/docs/current/runtime-config-logging.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;save it and restart Postgres or exit container by typing &lt;code&gt;exit&lt;/code&gt; then restart Postgres container instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker restart NAME_OF_CONTAINER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to test with your API !!&lt;/p&gt;

&lt;p&gt;Log CSV file should be in &lt;code&gt;pg_log&lt;/code&gt; directory in this path &lt;code&gt;/var/lib/postgresql/data/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To copy CSV file from container Docker to your computer use this command.&lt;br&gt;
eg. I gonna copy to ~/Downloads/docker-log directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker cp NAME_OF_CONTAINER:/var/lib/postgresql/data/pg_log ~/Downloads/docker-log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I recommend you add a header column to CSV file that we got so it's easy to implement and use a tool like &lt;a href="https://github.com/wireservice/csvkit"&gt;csvkit&lt;/a&gt; to filter data.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
  </channel>
</rss>
