Starting Postgres in Docker

If you'd like to start Postgres on your machine in under a minute, then the fastest and cross-platform approach is to bootstrap a database container in Docker.

Prerequisite

If Docker is not installed on your machine yet, then pause here and install it first. Check out Docker Desktop which is available for major operating systems and comes with a GUI that makes it easy to manage running containers.

Starting Postgres container

Once Docker is up and running, use the following command to start a Postgres container on Linux or macOS.

docker volume create postgres-volume
 
docker run --name postgres \
    -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password \
    -p 5432:5432 \
    -v postgres-volume:/var/lib/postgresql \
    -d postgres:latest

If you're a Windows user, run the following command in PowerShell. If you use Command Prompt (CMD), replace the backtick (`) with a caret (^) at the end of each line.

docker volume create postgres-volume
 
docker run --name postgres `
    -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password `
    -p 5432:5432 `
    -v postgres-volume:/var/lib/postgresql `
    -d postgres:latest

Confirm that the database is up and running by checking the logs:

docker logs postgres

The output should include the following messages:

listening on IPv4 address "0.0.0.0", port 5432
listening on IPv6 address "::", port 5432
...
database system is ready to accept connections

Connecting with psql

You can connect to the container using psql, a lightweight command-line tool that is included in the standard database distribution. The tool is already available inside the container.

Use this command to open a connection:

docker exec -it postgres psql -U postgres

After successful connection, psql will welcome you with a prompt similar to the following:

psql (18.1 (Debian 18.1-1.pgdg13+2))
Type "help" for help.
 
postgres=#

Now you can go ahead and run SQL commands against Postgres!

Warning

The way we deploy Postgres in Docker is suitable for development and for exploring the database's capabilities. However, if you plan to use this deployment option in production, be sure to review security and other best practices.