DEV Community

Cover image for Setting Up PostgreSQL with Docker: A Beginner's Guide
BHARGAV THAKAR
BHARGAV THAKAR

Posted on

Setting Up PostgreSQL with Docker: A Beginner's Guide

Hey there, fellow developers! πŸ‘‹ Today, we're going to walk through setting up PostgreSQL using Docker. If you've been wanting to learn Docker or need a quick way to spin up a PostgreSQL database for your projects, this guide is for you!

What We'll Cover πŸ—ΊοΈ

  • Installing Docker
  • Setting up PostgreSQL in a container
  • Basic PostgreSQL operations
  • Common troubleshooting tips

Prerequisites

  • A Linux-based system (I'm using Ubuntu)
  • Basic command-line knowledge
  • Terminal access
  • Internet connection

1. Installing Docker 🐳

First things first, let's get Docker installed on your system. Open your terminal and run these commands:

sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

These commands will:

  1. Update your package list
  2. Install Docker
  3. Start the Docker service
  4. Enable Docker to start on boot

To verify your installation:

docker --version
Enter fullscreen mode Exit fullscreen mode

You should see something like Docker version 20.10.21 (version numbers may vary).

2. Setting Up PostgreSQL 🐘

Now that we have Docker running, let's pull the official PostgreSQL image:

docker pull postgres
Enter fullscreen mode Exit fullscreen mode

You can verify the download with:

docker images
Enter fullscreen mode Exit fullscreen mode

Creating Your PostgreSQL Container

Here's where the magic happens. We'll create a new PostgreSQL container with some custom configurations:

docker run --name my_postgres \
    -e POSTGRES_USER=myuser \
    -e POSTGRES_PASSWORD=mypassword \
    -e POSTGRES_DB=mydatabase \
    -p 5432:5432 \
    -d postgres
Enter fullscreen mode Exit fullscreen mode

Let's break down what each flag means:

  • --name my_postgres: Names our container
  • -e POSTGRES_USER=myuser: Sets the database user
  • -e POSTGRES_PASSWORD=mypassword: Sets the user password
  • -e POSTGRES_DB=mydatabase: Creates an initial database
  • -p 5432:5432: Maps the container's PostgreSQL port to our host
  • -d: Runs the container in detached mode

Verify your container is running:

docker ps
Enter fullscreen mode Exit fullscreen mode

3. Working with PostgreSQL πŸ’½

Connecting to Your Database

To connect to your PostgreSQL instance:

docker exec -it my_postgres psql -U myuser -d mydatabase
Enter fullscreen mode Exit fullscreen mode

Essential PostgreSQL Commands

Here are some commands you'll use frequently:

Command Description
\l List all databases
CREATE DATABASE newdatabase; Create a new database
\c newdatabase Switch to a database
\dt List all tables
\q Exit postgres

Changing the User Password

If you need to change the password:

docker exec -it my_postgres psql -U myuser -d mydatabase
ALTER USER myuser WITH PASSWORD 'newpassword';
Enter fullscreen mode Exit fullscreen mode

4. Cleanup 🧹

When you're done, here's how to clean up:

# Stop the container
docker stop my_postgres

# Remove the container
docker rm my_postgres

# Remove the PostgreSQL image
docker rmi postgres
Enter fullscreen mode Exit fullscreen mode

Pro Tips πŸ’‘

  1. Use Docker Volumes: For data persistence, consider adding a volume:
   docker run -v postgres_data:/var/lib/postgresql/data ...
Enter fullscreen mode Exit fullscreen mode
  1. Strong Passwords: In production, use strong passwords and environment variables:
   docker run -e POSTGRES_PASSWORD=$MY_SECURE_PASSWORD ...
Enter fullscreen mode Exit fullscreen mode
  1. Custom Configuration: You can mount custom PostgreSQL configuration files:
   docker run -v my_postgres.conf:/etc/postgresql/postgresql.conf ...
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues πŸ”§

Permission Denied?

If you get permission errors, you might need to add your user to the docker group:

sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

Remember to log out and back in for changes to take effect!

Container Won't Start?

Check the logs:

docker logs my_postgres
Enter fullscreen mode Exit fullscreen mode

Conclusion

And there you have it! You now have a PostgreSQL database running in Docker. This setup is perfect for development environments and can be easily modified for production use.

What's Next?

  • Learn about Docker Compose for multi-container applications
  • Explore PostgreSQL backup strategies
  • Set up a pgAdmin container for GUI database management

Resources πŸ“š


Top comments (0)