AI Hints
Home›PostgreSQL Tutorial›What Is PostgreSQL
PostgreSQL · Lesson 2 of 120

Running Postgres in Docker

One command.

⏱ 2 min read▶ Video at 0:57🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

The easiest way to run Postgres is with Docker. One command downloads it and starts a database server. No installer, and it's easy to delete later.

  • One command
  • No installer
  • Easy cleanup

Start a Postgres server

Docker run starts a container named pg. The password is set with an environment variable. Port five four three two is Postgres's standard port. And the image is postgres, version eighteen.

Terminalbash
docker run -d --name pg \
  -e POSTGRES_PASSWORD=postgres \
  -p 5432:5432 \
  postgres:18

Open psql

Psql is Postgres's own command line tool. Docker exec runs it inside the container. You get a prompt, and you're talking to the database.

Terminalbash
docker exec -it pg psql -U postgres

psql (18.7)
Type "help" for help.

postgres=#

Summary

Docker run starts Postgres, and psql connects to it. Next, your first query.

Key takeaways
  • docker run
  • Port 5432
  • psql
Next up: psql: Your First Query — hello, Postgres