<?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: Genki Sugawara</title>
    <description>The latest articles on DEV Community by Genki Sugawara (@winebarrel).</description>
    <link>https://dev.to/winebarrel</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4107904%2F4c25ce77-4bf9-49ae-8d7d-1e1e23aca1cc.jpg</url>
      <title>DEV Community: Genki Sugawara</title>
      <link>https://dev.to/winebarrel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/winebarrel"/>
    <language>en</language>
    <item>
      <title>I built pistachio, a declarative schema tool for PostgreSQL</title>
      <dc:creator>Genki Sugawara</dc:creator>
      <pubDate>Fri, 04 Sep 2026 14:24:26 +0000</pubDate>
      <link>https://dev.to/winebarrel/i-built-pistachio-a-declarative-schema-tool-for-postgresql-5eh8</link>
      <guid>https://dev.to/winebarrel/i-built-pistachio-a-declarative-schema-tool-for-postgresql-5eh8</guid>
      <description>&lt;p&gt;I built &lt;a href="https://github.com/winebarrel/pistachio" rel="noopener noreferrer"&gt;pistachio&lt;/a&gt;, a declarative schema management tool for PostgreSQL. You write the schema you want in SQL, and the CLI generates the DDL diff against the current database and applies it in a Terraform-like plan/apply workflow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;-- added this line&lt;/span&gt;
    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;users_pkey&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add that one line to the schema file, and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;pista&lt;/span&gt; &lt;span class="n"&gt;plan&lt;/span&gt; &lt;span class="k"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;sql&lt;/span&gt;
&lt;span class="c1"&gt;-- Connected to postgres://postgres@localhost/postgres&lt;/span&gt;
&lt;span class="c1"&gt;-- Plan for schema public (1 table, 0 views, 0 enums, 0 domains, 0 composite types, 0 sequences)&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;pista&lt;/span&gt; &lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="k"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;sql&lt;/span&gt;
&lt;span class="c1"&gt;-- Connected to postgres://postgres@localhost/postgres&lt;/span&gt;
&lt;span class="c1"&gt;-- Apply to schema public (1 table, 0 views, 0 enums, 0 domains, 0 composite types, 0 sequences)&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Apply finished in 43ms&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;pista&lt;/span&gt; &lt;span class="n"&gt;plan&lt;/span&gt; &lt;span class="k"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;sql&lt;/span&gt;
&lt;span class="c1"&gt;-- Connected to postgres://postgres@localhost/postgres&lt;/span&gt;
&lt;span class="c1"&gt;-- Plan for schema public (1 table, 0 views, 0 enums, 0 domains, 0 composite types, 0 sequences)&lt;/span&gt;
&lt;span class="c1"&gt;-- No changes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;plan&lt;/code&gt; shows the diff, &lt;code&gt;apply&lt;/code&gt; runs it, and a second &lt;code&gt;plan&lt;/code&gt; reports no changes. Instead of piling up migration files, you keep editing one schema file as the desired state.&lt;/p&gt;

&lt;p&gt;Here it is in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F8ceaef33-7d4e-4bd8-bf94-1a79342cf1e1" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fuser-attachments%2Fassets%2F8ceaef33-7d4e-4bd8-bf94-1a79342cf1e1" alt="demo" width="720" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I built it
&lt;/h2&gt;

&lt;p&gt;I started with &lt;a href="https://github.com/sqldef/sqldef" rel="noopener noreferrer"&gt;sqldef&lt;/a&gt;, a tool built around the same concept, but it failed to parse some of the SQL in my development schema. I also considered &lt;a href="https://github.com/pgplex/pgschema" rel="noopener noreferrer"&gt;pgschema&lt;/a&gt;, but it has PostgreSQL itself interpret the desired schema, which means you need an embedded PostgreSQL or a dev server. That was heavier to operate than I wanted, so I skipped it. Instead I built pistachio, which parses the schema with &lt;a href="https://github.com/pganalyze/pg_query_go" rel="noopener noreferrer"&gt;pg_query_go&lt;/a&gt;, PostgreSQL's own parser packaged as a library. If PostgreSQL can parse it, pistachio can, and the tool stays lightweight.&lt;/p&gt;

&lt;p&gt;That said, sqldef is better if you work with multiple databases, and I think pgschema is more accurate at interpreting SQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;The desired schema is parsed into the same syntax tree PostgreSQL itself would build. pistachio reads the current schema straight from &lt;code&gt;pg_catalog&lt;/code&gt;, then compares the two and generates the DDL.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it manages
&lt;/h2&gt;

&lt;p&gt;pistachio currently manages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tables and columns&lt;/li&gt;
&lt;li&gt;constraints and indexes&lt;/li&gt;
&lt;li&gt;views and materialized views&lt;/li&gt;
&lt;li&gt;enum, domain, and composite types&lt;/li&gt;
&lt;li&gt;sequences&lt;/li&gt;
&lt;li&gt;row-level security and policies&lt;/li&gt;
&lt;li&gt;triggers&lt;/li&gt;
&lt;li&gt;comments and storage parameters&lt;/li&gt;
&lt;li&gt;functions and procedures (opt-in)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Renames go through the &lt;code&gt;-- pista:renamed-from&lt;/code&gt; directive and cover tables, columns, constraints, indexes, enum values, and so on. The details are in &lt;a href="https://winebarrel.github.io/pistachio/reference/objects/" rel="noopener noreferrer"&gt;Supported objects&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dump round trip
&lt;/h2&gt;

&lt;p&gt;pistachio is built so that feeding the output of &lt;code&gt;pista dump&lt;/code&gt; back as the desired schema makes plan report no changes. If that round trip breaks, it is a bug.&lt;/p&gt;

&lt;p&gt;CI dumps and re-plans dozens of schemas taken from real open source projects. A separate test suite loads the output of &lt;code&gt;pista dump&lt;/code&gt; into an empty database and checks that &lt;code&gt;pg_dump&lt;/code&gt; there matches the original. If dump and plan miss the same thing, the round-trip test alone would still pass.&lt;/p&gt;

&lt;p&gt;To adopt it on an existing database, run &lt;code&gt;pista dump &amp;gt; schema.sql&lt;/code&gt; and edit that file from then on.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it doesn't manage
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;CREATE EXTENSION&lt;/code&gt; / &lt;code&gt;CREATE ROLE&lt;/code&gt; / &lt;code&gt;GRANT&lt;/code&gt; are out of scope. They sit at a different privilege layer than the schema, and the role that runs a migration is usually not the role that owns the cluster, so I think they belong with the rest of the infrastructure, in Terraform for example.&lt;/p&gt;

&lt;p&gt;A statement pistachio doesn't manage is not dropped silently: each one gets an &lt;code&gt;ignored unsupported statement:&lt;/code&gt; warning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it with Docker
&lt;/h2&gt;

&lt;p&gt;There is a demo image that bundles PostgreSQL with a sample schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; ghcr.io/winebarrel/pistachio-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It starts a shell in &lt;code&gt;/demo&lt;/code&gt; with a preloaded PostgreSQL and a preconfigured &lt;code&gt;pista&lt;/code&gt;. Edit &lt;code&gt;desired.sql&lt;/code&gt;, then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pista plan  desired.sql     &lt;span class="c"&gt;# show the DDL diff&lt;/span&gt;
pista apply desired.sql     &lt;span class="c"&gt;# apply it&lt;/span&gt;
pista plan  desired.sql     &lt;span class="c"&gt;# reports no changes&lt;/span&gt;
pista dump                  &lt;span class="c"&gt;# dump the current schema&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Install with &lt;code&gt;brew install winebarrel/pistachio/pistachio&lt;/code&gt; or grab a binary from &lt;a href="https://github.com/winebarrel/pistachio/releases" rel="noopener noreferrer"&gt;Releases&lt;/a&gt;. The documentation is &lt;a href="https://winebarrel.github.io/pistachio/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>postgres</category>
      <category>database</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
