DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Streamlining Database Management: Running PostgreSQL in Docker Containers
  • Getting Started With Postgres: Three Free and Easy Ways
  • How To Run the Latest Version of PostgreSQL Using Docker
  • Lessons Learned Moving From On-Prem to Cloud Native

Trending

  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • How to Create a Successful API Ecosystem
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. PostgreSQL With Docker – Quick Start

PostgreSQL With Docker – Quick Start

In this post, I will show you how to easily run your database inside a container.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Oct. 30, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
18.8K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Docker is changing how we distribute and install software and it is a very popular tool in many areas of software development as well. In this post, I will show you how to quickly get started with docker and PostgreSQL.

I will try to keep this post very simple and will not cover complex workflows (those will be covered in later posts) and this will keep the discussion focus and help in easy learning. Now the idea is that you are gonna get, I don’t know, lights up the old neurons and creates one of those aha moments.

I am assuming that you already have installed docker on your machine and it is running.

Getting the Image

The following command will pull down the latest stable release Postgres image from the official Postgres docker hub repository.

>>docker pull postgres

Persist Data

Container data is gone once it is stopped and this is useful for certain situations (e.g. if you are running some database/integration testing and want to get rid of test data then it's great). But if we want to persist data generated by the Postgres instance running inside a container beyond the container’s lifecycle, we need to map a local mount point as a data volume to an appropriate path inside the container.

Running the Container

Java
 




x


 
1
>>docker run –name pg-docker -e POSTGRES_PASSWORD=docker -e PGDATA=/tmp -d -p 5433:5432 -v ${PWD}:/var/lib/postgresql/data postgres:11



The following command variation uses another environmental variable to setup the database as well:

Java
 




xxxxxxxxxx
1


 
1
>>docker run --name pg-docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=sampledb -e PGDATA=/tmp -d -p 5433:5432 -v ${PWD}:/var/lib/postgresql/data postgres:11



Connect and Use PostgreSQL

Once the container is up an running, connecting to it from an application is no different than connecting to a Postgres instance running outside a docker container. For example, to connect using psql we can execute

>>psql -h localhost -U postgres -d postgres

or u can use Azure Data Studio and make the connection as shown below:

We can also run psql inside the container by using the following command:

>>docker exec -it pg-docker psql -U postgres


We can issue SQL commands via docker cli as follows:

>>docker exec -it pg-docker psql -U postgres -c "CREATE DATABASE testdb;"

we can also run sql-script in a similar manner as shown below:

>>docker exec -it pg-docker psql -U postgres -f /opt/scripts/test_script.sql

Docker File

You can run the above-mentioned commands individually, but typically, you will create a docker file to build an image. Let's create a docker file to structure our setup requirements.


Notice the Copy command which is copying the script files from host directory to container.

I have two simple SQL script files as follows:

and here are the content of these script files, feel free to adjust as per your requirements: 

2-createtable.sql


3-insertdata.sql


Giving the file names numeric ascending value helps in controlling the execution order.

Now having all this in place, we can now build an image using this docker file:

>>docker image build -t postgresbasic .

Java
 




xxxxxxxxxx
1


 
1
>>docker run --name pg-docker -e PGDATA=/tmp -d -p 5433:5432 -v ${PWD}:/var/lib/postgresql/data postgresbasic



and finally, we can now check the database using psql or Azure Data Studio.


Summary

This was a basic introduction of how to use docker for running PostgreSQL database. We also saw a simple configuration related to environment setup and how to execute scripts as well. Docker file and scripts can be downloaded from this git repository. If you want to know more about Azure Data Studio or SQL, you can check my other articles as well. Let me know if you have any questions. Till next time, happy coding.

References

  • https://dzone.com/articles/fun-with-sql-using-postgres-and-azure-data-studio
  • https://hub.docker.com/_/postgres
  • https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres
  • https://hackernoon.com/dont-install-postgres-docker-pull-postgres-bee20e200198
Docker (software) PostgreSQL

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Streamlining Database Management: Running PostgreSQL in Docker Containers
  • Getting Started With Postgres: Three Free and Easy Ways
  • How To Run the Latest Version of PostgreSQL Using Docker
  • Lessons Learned Moving From On-Prem to Cloud Native

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!