<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abdul Basit on Medium]]></title>
        <description><![CDATA[Stories by Abdul Basit on Medium]]></description>
        <link>https://medium.com/@basit26374?source=rss-821a8040eb6f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6cdGotPPMR_1r9G19xQD5w.jpeg</url>
            <title>Stories by Abdul Basit on Medium</title>
            <link>https://medium.com/@basit26374?source=rss-821a8040eb6f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:00:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@basit26374/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to run PostgreSQL in Docker Container with Volume bound]]></title>
            <link>https://medium.com/@basit26374/how-to-run-postgresql-in-docker-container-with-volume-bound-c141f94e4c5a?source=rss-821a8040eb6f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c141f94e4c5a</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[pgadmin-4]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Abdul Basit]]></dc:creator>
            <pubDate>Wed, 10 Aug 2022 19:11:23 GMT</pubDate>
            <atom:updated>2022-08-21T06:01:09.717Z</atom:updated>
            <content:encoded><![CDATA[<h4>A handy guide to run Postgres Database in Containerized environment</h4><h3>Running PostgreSQL in Docker Container with Volume</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*b5UBEuktS0lhRoit39GtGw.jpeg" /></figure><p>First of all I would like to share my motivation behind this article why am writing this and believe this write up would be useful for others.</p><p>Whenever you work on containerized applications or want to do some learning around PostgreSQL, you may need to connect with multiple version of PostgreSQL in you local machine at the same time. In those cases, you need to know how to configure database in Docker.</p><p>Few Assumptions:</p><ul><li>Before beginning, I assume you already have <a href="https://www.docker.com/">docker</a> and <a href="https://www.pgadmin.org/download/">PgAdmin</a> in you machine.</li><li>Steps are almost same for Mac OS X, Windows and Ubuntu but I’ll show you the steps on Ubuntu 20.04</li></ul><p>What we will learned today in this article;</p><ol><li>Pull PostgreSQL image</li><li>Run Container</li><li>Create User via Docker Interactive Shell</li><li>Mount Docker volume with PostgreSQL container</li></ol><h4>Pull PostgreSQL image</h4><p>Its good idea to pull latest Postgres image from Docker hub. Here is the link if you want to learn more about the official postgres docker image <a href="https://hub.docker.com/_/postgres">https://hub.docker.com/_/postgres</a></p><p>But you can call ‘’pull postgres’’ docker image directly from docker CLI</p><pre>docker pull postgres</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*03WaycqMYUKWo2Lbo2jFJw.png" /><figcaption>docker pull postgres</figcaption></figure><p>By default you’ll pull latest image but if you want to work on any specific version then you can run</p><pre>docker pull postgres:&lt;version_tag&gt;</pre><p>If downloading image in your machine is successfully completed then you can verify</p><pre>docker images -a</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*la4PMBlrHVv2HAl_Lg8Orw.png" /><figcaption>list all docker images in local machine</figcaption></figure><h4>Run Container</h4><p>Here is the nifty command to run postgreSQL container within a second</p><pre>docker run --name learn_postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5433:5432 -d postgres</pre><p>Let me explain attributes one by one</p><p>Using the <strong>--name</strong> attribute allows you to assign a container name. In our case, I chose learn_postgres</p><p>There are two ways of running a container — in <strong>attached mode</strong> (in the foreground) or in <strong>detached mode</strong> (in the background).</p><p>By default, Docker runs the container in attached mode. Meaning it’s attached to the terminal session, where it displays output and messages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yM3TyOLDNBnLWTvySPIoyg.png" /></figure><p>We use the <strong>-d</strong> flag to detach the container from our terminal and run it in the background.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i6i4-b3bKdf9LLJUmSrCEQ.png" /></figure><p>The <strong>-e</strong> flag lets you specify an environment variable. By default postgres image has default postgresuser and password mysecretpassword.</p><p>When you run a container, the only way to access the process is from inside of it. To allow external connections to the container, you have to open (publish) specific ports.</p><p>You have to add the <strong>-p</strong> option to the <strong>docker run</strong> command as well as the following</p><p>But what happens if you already have postgreSQL in your local machine and it also has postgres user? It’s going to conflict with each other.</p><p>So in that scenario you should create a new user in postgres docker contanier.</p><h4>Create User via Docker Interactive Shell</h4><p>Before going to create new user in docker container, first you need to enter into docker container via execcommand.</p><pre>docker exec -it learn_postgres bash</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ebCQoj9jVkmfiyx7Z49tfw.png" /></figure><p>If you want to start an interactive shell inside a Docker Container, perhaps to explore the filesystem or debug running processes, use docker exec with the -i and -t flags.</p><p>The -i flag keeps input-mode open even if not attached to the container, and the -t flag creates a pseudo-terminal that the shell can attach to.</p><p>To enter into the container, log in via postgres user</p><pre>su - postgres</pre><p>And then type psql</p><blockquote><strong>Psql</strong> is an interactive terminal to work with the PostgreSQL database</blockquote><p>See the list of all database users \du</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nClj9jhtt_wwE-KZXrfW-g.png" /></figure><p>We should create superuser with creation roles.</p><pre>CREATE ROLE docker_user with login SUPERUSER PASSWORD ‘docker_user’;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S5FxQc7tX0hzORKiVGpjCQ.png" /></figure><p>I created a superuser docker_user with password docker_userin my case. Feel free to chose a suitable name and strong password in your case.</p><p>After the role creation, we are adding more privileges to the user to create Database and so on.</p><pre>ALTER USER docker_user WITH CREATEDB CREATEROLE;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IFv8mZhU08fJtAH-m6uJ_A.png" /></figure><p>That’s it. Now you can quit from database connection using psql command \q and then type exit to logged out postgres user. And then type another time exit to leave out from docker container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LTjDJpeH3qaRed7H4lna_Q.png" /></figure><p>Now things are done and we get to know how to run postgres container.</p><pre>docker run --name learn_postgres -e POSTGRES_PASSWORD=docker_user -e POSTGRES_USER=docker_user -p 5433:5432 -d postgres</pre><p>Once we spin up the container, we get container ID</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ic8YFAB_o0SRkOAuLjQE3A.png" /><figcaption>docker run</figcaption></figure><p>Let’s try to connect with PgAdmin4, create database schema and insert some dummy data.</p><p>Open the PgAdmin4 application in your machine and Add →Server. Now you can see the dialog box where you can provide server name as you wish. I chose dockerized_postgres and provided connection details with <strong>user</strong> and <strong>password</strong> docker_user and docker_userrespectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*q9Zfj1PnXcqL3iBr1UWweA.jpeg" /><figcaption>Add database and define the name of it</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*jfi9-DqWTRRY960OBJ3bYA.jpeg" /><figcaption>Database connection details</figcaption></figure><p>Create database with name let’s say, temporary</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*sDn4O1jR0gzucHsOKQTOag.jpeg" /><figcaption>Create database</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*FoAXEc0KuFJY6FgGU_XwCg.jpeg" /><figcaption>Put the name of database</figcaption></figure><p>Insert some dummy data in it</p><pre>CREATE TABLE accounts (<br>account_id serial PRIMARY KEY,<br>name VARCHAR (50) NOT NULL);</pre><pre>INSERT INTO accounts (account_id, name)<br>VALUES (1, ‘HBL’);</pre><pre>SELECT * from accounts;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TVPsvFXnzRVCVqwS7fr5uQ.png" /><figcaption>Run SQL queries on Containerized PostgreSQL</figcaption></figure><p>If all goes well, you can see the record after select query</p><p>Wait, Are you thinking about what happens if we stop the container and remove container learn_postgres? If you thought that you’ll lose the data then you are right. Yes data will be lost once container is removed.</p><p>Docker containers do not save the data they produce. As soon as the process is finished, the container stops and everything inside of it is removed.</p><p>If you want to have persistent data that is stored even after the container stops, you need to enable sharing storage volumes.</p><h4>Docker volume with PostgreSQL container</h4><p>So here we mount docker volume to persist data permanently.</p><p>First we create a docker volume and then we mount volume to the docker container in order to persist data.</p><pre>docker volume create postgres-volume</pre><p>Now we mount the docker volume to the container once we run the container.</p><pre>docker run --name learn_postgres -e POSTGRES_PASSWORD=docker_user -e POSTGRES_USER=docker_user -p 5433:5432 -v pgdata:/var/lib/postgresql/data -d postgres</pre><p>For <a href="https://phoenixnap.com/kb/docker-volumes#htoc-mounting-a-host-directory-as-a-data-volume">mounting volumes</a> use the <strong>-v</strong> attribute with the specified location of the directory where you want to save the data, followed by where that data will be located inside the container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ENTSMtSKl67bylaBWlNfrQ.png" /><figcaption>container run with volume mount</figcaption></figure><p>Now you are safe to create database and insert data into it. Again you create database and insert sample queries to put some dummy data into the database and see the data persistence due to docker volume.</p><blockquote>Connect with me on <a href="https://www.linkedin.com/in/abdul-basit-2g7/">linkedIn</a> if you are working with docker and wanted you discuss things on mutual interest tech topics.</blockquote><h4>References</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F-pzptvcJNh0%3Fstart%3D181%26feature%3Doembed%26start%3D181&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D-pzptvcJNh0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-pzptvcJNh0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bd4ae6f58e279e29040888e52aa462d/href">https://medium.com/media/8bd4ae6f58e279e29040888e52aa462d/href</a></iframe><ul><li><a href="https://phoenixnap.com/kb/docker-run-command-with-examples">How to Use Docker Run Command with Examples</a></li><li><a href="https://www.digitalocean.com/community/tutorials/how-to-use-docker-exec-to-run-commands-in-a-docker-container">How To Use docker exec to Run Commands in a Docker Container | DigitalOcean</a></li><li><a href="https://stackoverflow.com/questions/61623124/persist-data-on-postgres-docker-with-a-volume-when-restarting-the-container">Persist data on postgres docker with a volume when restarting the container</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c141f94e4c5a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>