<?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: Wedson Lima</title>
    <description>The latest articles on DEV Community by Wedson Lima (@wedsonlima).</description>
    <link>https://dev.to/wedsonlima</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%2F1279347%2Fa72b08c8-b6a0-4ace-97cb-a223fd204517.png</url>
      <title>DEV Community: Wedson Lima</title>
      <link>https://dev.to/wedsonlima</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wedsonlima"/>
    <language>en</language>
    <item>
      <title>Quick SQLite to PostgreSQL Migration with pgloader</title>
      <dc:creator>Wedson Lima</dc:creator>
      <pubDate>Thu, 01 May 2025 17:18:27 +0000</pubDate>
      <link>https://dev.to/wedsonlima/quick-sqlite-to-postgresql-migration-with-pgloader-3pna</link>
      <guid>https://dev.to/wedsonlima/quick-sqlite-to-postgresql-migration-with-pgloader-3pna</guid>
      <description>&lt;p&gt;&lt;a href="https://pgloader.readthedocs.io/en/latest/" rel="noopener noreferrer"&gt;pgloader&lt;/a&gt; is an open-source data loading tool that efficiently streams data into PostgreSQL using the COPY protocol. It handles schema discovery, data transformations, and error management automatically. For SQLite migrations, it can handle both schema and data in a single command, making it perfect for automated database migrations.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Restore SQLite from Litestream
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Set your AWS credentials and restore the backup&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;LITESTREAM_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"your_access_key"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
       &lt;span class="nv"&gt;LITESTREAM_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"your_secret_key"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
litestream restore &lt;span class="nt"&gt;-o&lt;/span&gt; storage/production.sqlite3 &lt;span class="se"&gt;\&lt;/span&gt;
  s3://your-bucket/storage/production.sqlite3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Migrate to PostgreSQL
&lt;/h2&gt;

&lt;p&gt;For cloud PostgreSQL (e.g., Supabase):&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; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-v&lt;/span&gt; /path/to/your/sqlite:/data/production.sqlite3 &lt;span class="se"&gt;\&lt;/span&gt;
  dimitri/pgloader:latest &lt;span class="se"&gt;\&lt;/span&gt;
  pgloader &lt;span class="nt"&gt;--with&lt;/span&gt; &lt;span class="s2"&gt;"DATA ONLY"&lt;/span&gt; &lt;span class="nt"&gt;--verbose&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    sqlite:///data/production.sqlite3 &lt;span class="se"&gt;\&lt;/span&gt;
    postgresql://user:password@your-postgres-host:6543/postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For local PostgreSQL:&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; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-v&lt;/span&gt; /path/to/your/sqlite:/data/production.sqlite3 &lt;span class="se"&gt;\&lt;/span&gt;
  dimitri/pgloader:latest &lt;span class="se"&gt;\&lt;/span&gt;
  pgloader &lt;span class="nt"&gt;--with&lt;/span&gt; &lt;span class="s2"&gt;"DATA ONLY"&lt;/span&gt; &lt;span class="nt"&gt;--verbose&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    sqlite:///data/production.sqlite3 &lt;span class="se"&gt;\&lt;/span&gt;
    postgresql://postgres:postgres@host.docker.internal:5432/your_db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;--with "DATA ONLY"&lt;/code&gt; if tables are already created in PostgreSQL&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;host.docker.internal&lt;/code&gt; to connect to local PostgreSQL from Docker&lt;/li&gt;
&lt;li&gt;Ensure your SQLite file path in the volume mount matches the path in the pgloader command&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Important Notes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Always backup your data before migration&lt;/li&gt;
&lt;li&gt;Test the migration process in a staging environment first&lt;/li&gt;
&lt;li&gt;Plan for downtime during the migration&lt;/li&gt;
&lt;li&gt;Update your application's database connection strings after migration&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>postgres</category>
      <category>sqlite3</category>
      <category>pgloader</category>
    </item>
    <item>
      <title>Active Storage: Retaining and removing uploaded files on form submission</title>
      <dc:creator>Wedson Lima</dc:creator>
      <pubDate>Mon, 12 Feb 2024 22:21:54 +0000</pubDate>
      <link>https://dev.to/wedsonlima/active-storage-retaining-and-removing-uploaded-files-on-form-submission-1hjf</link>
      <guid>https://dev.to/wedsonlima/active-storage-retaining-and-removing-uploaded-files-on-form-submission-1hjf</guid>
      <description>&lt;p&gt;Working with active storage, you may encounter a common problem when submitting the form; Rails will replace or remove all previously uploaded files.&lt;/p&gt;

&lt;p&gt;Here, I show a simple approach to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;not replace or delete the previously uploaded files on form submission;&lt;/li&gt;
&lt;li&gt;remove a specific file while editing a form;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will use the &lt;code&gt;signed_id&lt;/code&gt; from &lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/SignedId.html"&gt;active record&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# your model&lt;/span&gt;
&lt;span class="n"&gt;has_many_attached&lt;/span&gt; &lt;span class="ss"&gt;:files&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight slim"&gt;&lt;code&gt;&lt;span class="c"&gt;// _form.html&lt;/span&gt;
&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt; &lt;span class="ss"&gt;:files&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;input_html: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;class: &lt;/span&gt;&lt;span class="s1"&gt;'form-control'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;multiple: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;attached?&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt; &lt;span class="ss"&gt;:files&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;input_html: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;value: &lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signed_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;multiple: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="ss"&gt;as: :hidden&lt;/span&gt;

    &lt;span class="nt"&gt;li&lt;/span&gt;
      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rails_blob_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;disposition: &lt;/span&gt;&lt;span class="s1"&gt;'attachment'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;'&lt;/span&gt;
      &lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="nc"&gt;.btn.btn-danger.remove-uploaded-file&lt;/span&gt;[&lt;span class="na"&gt;data-uploaded-file-signed-id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="nc"&gt;.signed_id&lt;/span&gt;]&lt;span class="w"&gt; &lt;/span&gt;X
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.remove-uploaded-file&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;$parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;signedId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;uploaded-file-signed-id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$fileInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input[value="&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;signedId&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;"]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;$fileInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&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;This will give you something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figp9vj5sht6gassw79de.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figp9vj5sht6gassw79de.png" alt="Image description" width="800" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
    </item>
    <item>
      <title>How to work with multiple PostgreSQL versions at the same time without losing your mind</title>
      <dc:creator>Wedson Lima</dc:creator>
      <pubDate>Mon, 12 Feb 2024 20:55:45 +0000</pubDate>
      <link>https://dev.to/wedsonlima/how-to-work-with-multiple-postgresql-versions-at-the-same-time-without-losing-your-mind-12nk</link>
      <guid>https://dev.to/wedsonlima/how-to-work-with-multiple-postgresql-versions-at-the-same-time-without-losing-your-mind-12nk</guid>
      <description>&lt;h1&gt;
  
  
  Using Docker to work with multiple PostgreSQL versions
&lt;/h1&gt;

&lt;p&gt;How to work with multiple PostgreSQL versions at the same time without losing my mind?&lt;/p&gt;

&lt;p&gt;That's what we are trying to answer here. With &lt;strong&gt;Docker&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker PG Volumes
&lt;/h2&gt;

&lt;p&gt;Data saved in a docker container are lost when that container is destroyed. We can save that data using docker volumes.&lt;/p&gt;

&lt;p&gt;That's how we create a volume to use with a pg version 9.6:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker volume create pgdata96
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's how we create a volume for pg 13:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker volume create pgdata13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker PG Containers
&lt;/h2&gt;

&lt;p&gt;I like to map each pg version with a similar port. I follow this simple pattern:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;version&lt;/th&gt;
&lt;th&gt;port&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;postgres96&lt;/td&gt;
&lt;td&gt;54*&lt;em&gt;96&lt;/em&gt;*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;postgres12&lt;/td&gt;
&lt;td&gt;54*&lt;em&gt;12&lt;/em&gt;*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;postgres13&lt;/td&gt;
&lt;td&gt;54*&lt;em&gt;13&lt;/em&gt;*&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This command will download, run and setup for us a container with a postgres 9.6:&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;--name&lt;/span&gt; postgres96 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;123456 &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 5496:5432 &lt;span class="nt"&gt;-v&lt;/span&gt; pgdata96:/var/lib/postgresql/data postgres:9.6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the same for a postgres 13:&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;--name&lt;/span&gt; postgres13 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;123456 &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 5413:5432 &lt;span class="nt"&gt;-v&lt;/span&gt; pgdata13:/var/lib/postgresql/data postgres:13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look how I mapped the ports.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a DB
&lt;/h2&gt;

&lt;p&gt;Let's create a database called &lt;strong&gt;todolist&lt;/strong&gt; in the pg version 13:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; postgres13 psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"CREATE DATABASE todolist WITH OWNER=postgres;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can log in to the container e create it using &lt;em&gt;psql&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; postgres13 psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ CREATE DATABASE todolist;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Start the container
&lt;/h2&gt;

&lt;p&gt;When you restart your machine, or for some other reasons unknown, you can start the containers like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker start postgres13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need to run other containers you can run like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker start postgres13 redis memcached
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is no need to run the command &lt;code&gt;docker run&lt;/code&gt; again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Restore a dump
&lt;/h2&gt;

&lt;p&gt;That's how you can restore a local dump file into a database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; postgres13 psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-d&lt;/span&gt; todolist &lt;span class="nt"&gt;-f&lt;/span&gt; dump.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>postgres</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Configuring a PostgreSQL Database with Docker for a Rails App</title>
      <dc:creator>Wedson Lima</dc:creator>
      <pubDate>Mon, 12 Feb 2024 20:53:11 +0000</pubDate>
      <link>https://dev.to/wedsonlima/configuring-a-postgresql-database-with-docker-for-a-rails-app-157d</link>
      <guid>https://dev.to/wedsonlima/configuring-a-postgresql-database-with-docker-for-a-rails-app-157d</guid>
      <description>&lt;h4&gt;
  
  
  Introduction
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/get-started/"&gt;Docker&lt;/a&gt; and &lt;a href="https://docs.docker.com/compose/gettingstarted/"&gt;Docker Compose&lt;/a&gt; are indispensable tools in modern development workflows. They simplify the setup and management of development environments, ensuring consistency across various setups. There are a plethora of resources available for understanding Docker and Docker Compose, and their installation guides can be found &lt;a href="https://docs.docker.com/get-docker/"&gt;here&lt;/a&gt; and &lt;a href="https://docs.docker.com/compose/install/"&gt;here&lt;/a&gt; respectively. Since these topics are well explored, we’ll dive straight into configuring a PostgreSQL database for your Rails project using Docker Compose, emphasizing PostgreSQL version specification and managing separate volumes for different versions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Prerequisites
&lt;/h4&gt;

&lt;p&gt;Before we dive in, ensure you have the following set up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker and Docker Compose installed on your machine.&lt;/li&gt;
&lt;li&gt;A Rails project set up and ready to be configured with a PostgreSQL database.&lt;/li&gt;
&lt;li&gt;A basic understanding of Rails and PostgreSQL.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 1: Installing Docker and Docker Compose
&lt;/h4&gt;

&lt;p&gt;If you haven't installed Docker and Docker Compose yet, follow the official guides linked in the introduction. Once installed, verify the installations by running the following commands in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nt"&gt;--version&lt;/span&gt;
docker-compose &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2: Defining Volumes
&lt;/h4&gt;

&lt;p&gt;In Docker, volumes are utilized for data persistence across container restarts and sharing data among containers. They are paramount for databases to ensure that data remains intact even when containers are stopped or removed. For our PostgreSQL setup, having separate volumes for different versions of PostgreSQL is beneficial to avoid any data conflicts or corruption when switching between different versions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres_data_16&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres_data_15&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the snippet above, we define separate volumes for PostgreSQL versions 16, 13, and 12.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Creating a Docker Compose File
&lt;/h4&gt;

&lt;p&gt;Docker Compose facilitates the management of multi-container Docker applications. We'll create a &lt;code&gt;docker-compose.yml&lt;/code&gt; file to define our PostgreSQL service, specifying the version and linking the appropriate volume for data storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:16&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myapp_development&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myappuser&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myapppassword&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;postgres_data_16:/var/lib/postgresql/data&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;5416:5432"&lt;/span&gt;
&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres_data_16&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres_data_15&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this configuration, we've specified the PostgreSQL version as 16, defined the database user and password, mounted the &lt;code&gt;postgres_data_16&lt;/code&gt; volume to the &lt;code&gt;/var/lib/postgresql/data&lt;/code&gt; directory inside the container, and mapped the container's port 5432 to the host's port 5160.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: Setting up the Procfile.dev
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;Procfile.dev&lt;/code&gt; is a manifest used in Rails projects to define the services required for the development environment. Create a &lt;code&gt;Procfile.dev&lt;/code&gt; file in the root of your project with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: bundle exec rails s
db: docker-compose up db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration specifies three services: the Rails server, Sidekiq for background jobs, and our Docker Compose-managed PostgreSQL database.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5: Starting Your Development Environment
&lt;/h4&gt;

&lt;p&gt;With all configurations in place, use Foreman to start your development environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;foreman start &lt;span class="nt"&gt;-f&lt;/span&gt; Procfile.dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Foreman will launch the services defined in &lt;code&gt;Procfile.dev&lt;/code&gt;, getting your development environment up and running.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;We've streamlined the process of configuring a PostgreSQL database for a Rails project using Docker Compose. This setup not only simplifies the database management but also ensures consistency across different development environments. Feel free to adapt this setup to your projects, and explore further customizations to tailor the environment to your needs.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>postgres</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
