DEV Community

Cover image for How to Set Up Postgres Using Docker
Ayra Jett for Bytebase

Posted on • Originally published at bytebase.com

4 2 3 3 2

How to Set Up Postgres Using Docker

Step 1: Pull the Official PostgreSQL Image

We need to pull the official PostgreSQL image from Docker Hub. Open your terminal or command prompt and run:

docker pull postgres
# -- Instead, for a certain version of postgres, e.g. 14.5, run:
# docker pull postgres:14.5
Enter fullscreen mode Exit fullscreen mode

And wait for the latest version of the PostgreSQL image to be downloaded.

Image description

Step 2: Create and Run a PostgreSQL Container

Now that we've downloaded the image, we can create and run a PostgreSQL container:

docker run -d --name mypostgres -p 5432:5432 -e POSTGRES_PASSWORD=yourpassword postgres
Enter fullscreen mode Exit fullscreen mode

This command runs the container in detached mode in the background, assigns a name to it, maps the container's port 5432 to the host's port 5432, sets the password for the default postgres user, and specifies the image to use for creating the postgres container.

Check whether the PostgreSQL container is running with this command:

docker ps
Enter fullscreen mode Exit fullscreen mode

Image description

Step 3: Connect to the PostgreSQL Database and Operate

Connect from the host machine

You can use the psql cammand to connect to your database.

psql -h localhost -U postgres
Enter fullscreen mode Exit fullscreen mode

Connect within Docker

Or open an interactive terminal inside the container and connect to the PostgreSQL database with the postgres user.

docker exec -it mypostgres psql -h localhost -U postgres
Enter fullscreen mode Exit fullscreen mode

Then we can operate with SQL command after the "#".

Image description

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more