<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Postgres on Ye Joo Park's Blog</title><link>https://park.is/categories/postgres/</link><description>Recent content in Postgres on Ye Joo Park's Blog</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Thu, 04 Nov 2021 00:00:00 +0000</lastBuildDate><atom:link href="https://park.is/categories/postgres/index.xml" rel="self" type="application/rss+xml"/><item><title>Duplicating a Supabase Postgres Schema to a New Project</title><link>https://park.is/blog_posts/20211104_duplicate_a_supabase_database/</link><pubDate>Thu, 04 Nov 2021 00:00:00 +0000</pubDate><guid>https://park.is/blog_posts/20211104_duplicate_a_supabase_database/</guid><description>&lt;p&gt;Looking to duplicate a Postgres database in a Supabase project to another project? Using &lt;code&gt;pg_dump&lt;/code&gt; and &lt;code&gt;pg_restore&lt;/code&gt; won&amp;rsquo;t work because you only have superuser access through the Supabase dashboard. Here&amp;rsquo;s a workflow of duplicating a Supabase Postgres schema using pgAdmin and the Supabase dashboard.&lt;/p&gt;&#10;&lt;h2 id="exporting-schema"&gt;Exporting Schema&lt;/h2&gt;&#10;&lt;p&gt;You can use the &lt;code&gt;pg_dump&lt;/code&gt; in a CLI as well.&lt;/p&gt;&#10;&lt;h3 id="right-click-on-pgadmins-public-schema-and-click-backup"&gt;Right-click on pgAdmin&amp;rsquo;s public schema and click Backup&lt;/h3&gt;&#10;&lt;p&gt;&lt;img src="https://user-images.githubusercontent.com/1064036/140382400-0792e227-4d7b-48e1-bfc1-bba413f1038e.png" alt="image"&gt;&lt;/p&gt;&#10;&lt;h3 id="enter-a-filename-and-choose-plain-format"&gt;Enter a filename and choose Plain format&lt;/h3&gt;&#10;&lt;p&gt;&lt;img src="https://user-images.githubusercontent.com/1064036/140382436-ca1b821f-0f44-4d58-a537-863569f3ad5b.png" alt="image"&gt;&lt;/p&gt;</description></item><item><title>PostgreSQL Trigger: Calculate the Difference between Two Timestamps in Seconds</title><link>https://park.is/blog_posts/20210605_postgres_trigger_time_between_two_dates/</link><pubDate>Sat, 05 Jun 2021 00:00:00 +0000</pubDate><guid>https://park.is/blog_posts/20210605_postgres_trigger_time_between_two_dates/</guid><description>&lt;p&gt;A SQL trigger is a function that is automatically invoked when an event occurs. To set a trigger, you specify the following:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;which table(s) and column(s) to listen to&lt;/li&gt;&#10;&lt;li&gt;which actions to listen to (e.g., &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;)&lt;/li&gt;&#10;&lt;li&gt;what function or procedure to invoke.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Triggers have many use cases. Some examples are:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Create an audit trail&lt;/strong&gt;: You have a table that contains sensitive information. You create a trigger to record all changes (what change has been made, which user has made the change, when the change has been made) to a separate table.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Integrity check&lt;/strong&gt;: Before adding a student to a class roster, you want to ensure that the student&amp;rsquo;s previous course enrollments fulfill prerequisites.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Derive additional data&lt;/strong&gt;: You have a table that contains the start and the end time of an online exam for test-takers. When a student is done with the exam, you want to calculate how long the student took to complete the exam using the start and end timestamps. Note that this can also be implemented using a generated column.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This post will implement the last example. You create a test-taking web app where you mark the start time (&lt;code&gt;start_time&lt;/code&gt;) when the student hits the &amp;ldquo;Start&amp;rdquo; button. Once the student finishes taking the exam and clicks on &amp;ldquo;Submit&amp;rdquo;, the web app will mark the finish time (&lt;code&gt;end_time&lt;/code&gt;). You want to automatically calculate how long the test-taker spent on the exam in &lt;strong&gt;seconds&lt;/strong&gt;.&lt;/p&gt;</description></item></channel></rss>