<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Database on entangledDEV</title>
        <link>/tags/database/</link>
        <description>Recent content in Database on entangledDEV</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <lastBuildDate>Sun, 01 Feb 2026 00:09:25 -0600</lastBuildDate><atom:link href="/tags/database/index.xml" rel="self" type="application/rss+xml" /><item>
            <title>How to Create Databases Using Docker</title>
            <link>/p/how-to-create-databases-using-docker/</link>
            <pubDate>Mon, 18 Sep 2023 19:47:33 -0600</pubDate>
            <guid>/p/how-to-create-databases-using-docker/</guid>
            <description>&lt;p&gt;The other day, I needed to set up a PostgreSQL database, and after considering various solutions, I decided to use Docker. In this tutorial, I&amp;rsquo;ll share the solution that worked for my problem.&lt;/p&gt;&#xA;&lt;h2 id=&#34;creating-a-postgresql-database-using-docker-compose&#34;&gt;&lt;a href=&#34;#creating-a-postgresql-database-using-docker-compose&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Creating a PostgreSQL Database Using Docker Compose&#xA;&lt;/h2&gt;&lt;p&gt;Docker Compose is a powerful tool that allows us to manage multiple containers from a single central point. You might think it&amp;rsquo;s a bit much for setting up a database, but I found it surprisingly easy to set up and use.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s start!&lt;/p&gt;&#xA;&lt;h3 id=&#34;creating-the-docker-composeyml-file&#34;&gt;&lt;a href=&#34;#creating-the-docker-composeyml-file&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Creating the docker-compose.yml File&#xA;&lt;/h3&gt;&lt;p&gt;Docker Compose requires a configuration file named &lt;code&gt;docker-compose.yml&lt;/code&gt; where we define and configure our containers. Writing this file is straightforward; here&amp;rsquo;s the one I used for my database:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nt&#34;&gt;version&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;3&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nt&#34;&gt;services&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;  &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;postgres-db&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;l&#34;&gt;postgres:latest&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;container_name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;l&#34;&gt;postgres_container&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;volumes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;- &lt;span class=&#34;s2&#34;&gt;&amp;#34;/path_to_local_storage:/var/lib/postgresql/data&amp;#34;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;environment&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;- &lt;span class=&#34;l&#34;&gt;POSTGRES_DB=my_db&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;- &lt;span class=&#34;l&#34;&gt;POSTGRES_USER=user&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;- &lt;span class=&#34;l&#34;&gt;POSTGRES_PASSWORD=password&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;ports&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;      &lt;/span&gt;- &lt;span class=&#34;s1&#34;&gt;&amp;#39;5433:5432&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above file:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;We define a service named postgres-db containing the PostgreSQL database image.&lt;/li&gt;&#xA;&lt;li&gt;You can customize the container_name to your preference.&lt;/li&gt;&#xA;&lt;li&gt;To ensure data persistence and choose a custom storage location, define your preferred local path in the &amp;lsquo;volumes&amp;rsquo; section.&lt;/li&gt;&#xA;&lt;li&gt;Set your database credentials (user, password) and the default database name in the environment section.&lt;/li&gt;&#xA;&lt;li&gt;The port mapping is configured to use the default PostgreSQL ports. You can adjust this mapping if necessary; the format is &lt;code&gt;local_machine_port:docker port&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;starting-the-database&#34;&gt;&lt;a href=&#34;#starting-the-database&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Starting the database&#xA;&lt;/h3&gt;&lt;p&gt;To start your database, open a terminal in the directory where your &lt;code&gt;docker-compose.yml&lt;/code&gt; is located (or specify a relative path to the file) and run this command (assuming you&amp;rsquo;re using Docker Compose 2):&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;docker compose -p example up -d&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This command creates the containers defined in the compose file. You can customize the project name with the &lt;code&gt;-p&lt;/code&gt; property.&lt;/p&gt;&#xA;&lt;h3 id=&#34;stopping-the-database&#34;&gt;&lt;a href=&#34;#stopping-the-database&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Stopping the database&#xA;&lt;/h3&gt;&lt;p&gt;To stop your database, use this command:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;docker compose down&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;connecting-locally-to-the-database&#34;&gt;&lt;a href=&#34;#connecting-locally-to-the-database&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Connecting locally to the database&#xA;&lt;/h3&gt;&lt;p&gt;Connecting to the database locally on your computer is simple. Access &lt;code&gt;http://localhost:5433&lt;/code&gt; using your chosen username and password. While I use DataGrip to connect, any database software can connect to it.&lt;/p&gt;&#xA;&lt;h2 id=&#34;troubleshooting&#34;&gt;&lt;a href=&#34;#troubleshooting&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Troubleshooting&#xA;&lt;/h2&gt;&lt;p&gt;During the container creation process, I encountered some common issues, along with their solutions.&lt;/p&gt;&#xA;&lt;h3 id=&#34;issue-the-image-exited-with-code-1&#34;&gt;&lt;a href=&#34;#issue-the-image-exited-with-code-1&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Issue: The image exited with code 1.&#xA;&lt;/h3&gt;&lt;p&gt;If your container exits immediately when launched, check the container logs by running:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;docker logs my_container_name&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I discovered that my data directory contained hidden files that caused this issue. Deleting these hidden files allowed the database to start and function as expected.&lt;/p&gt;&#xA;&lt;h3 id=&#34;issue-the-mounting-point-is-not-valid&#34;&gt;&lt;a href=&#34;#issue-the-mounting-point-is-not-valid&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Issue: The mounting point is not valid.&#xA;&lt;/h3&gt;&lt;p&gt;If you encounter issues starting the image, it may be related to the local path name. If your path contains spaces, a simple workaround is to enclose the path in double quotes.&lt;/p&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;&lt;a href=&#34;#conclusion&#34; class=&#34;header-anchor&#34;&gt;&lt;/a&gt;Conclusion&#xA;&lt;/h2&gt;&lt;p&gt;By utilizing &lt;code&gt;docker-compose&lt;/code&gt;, we can easily start and manage data persistence for our databases. This method provides a straightforward and quick way to create databases with data persistence. I hope this tutorial proves as helpful to you as it was to me.&lt;/p&gt;&#xA;&lt;p&gt;Happy coding!&lt;/p&gt;&#xA;</description>
        </item></channel>
</rss>
