How to Run PostgreSQL on Docker?

Опубликовано: 14 Сентябрь 2024
на канале: Database Dive
31
0

In this PostgreSQL tutorial we're going to walk you through a PostgreSQL Docker installation.

PostgreSQL Docker installations are very frequent SQL interview questions, so make sure to brush up on such SQL interview questions and answers before attending your next SQL interview.

Indeed, installing PostgreSQL on a Docker infrastructure isn't as hard as perhaps some of you think! It's not necessary to familiarize yourself with docker architecture before following this tutorial.

Here are the steps to make everything happen:

1. Download and install Docker.
Download Docker from the official website, then proceed by installing it on your machine.

2. Download the Docker PostgreSQL image. Here's a command that helps you do just that:
docker pull postgres

To see existing Docker images, run:
docker images

3. To connect to PostgreSQL Docker instance through Docker, run:
docker run --name [username] -e POSTGRES_USER=[username] -e POSTGRES_PASSWORD=[password] -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres

In this command:
* -e POSTGRES_USER is the PostgreSQL user that accesses the database.
* -e POSTGRES_PASSWORD is the password of that user.
* -p 5432:5432 are used to establish communication between the host port and the Docker container port.

If both ports are "5432", all requests sent to the host ports will be automatically sent to the Docker container port.

* -v defines the data directory of PostgreSQL.
* -d runs the Docker container in the detached mode (in the background.)

"postgres" is the name of the Docker image that was downloaded to run the Docker container.

Make sure to subscribe to this database channel to gain more insight into this docker tutorial, docker vs kubernetes, database interview questions, sql interview questions and answers surrounding mysql server, the postgresql database, mysql vs nosql, and so on. Do you want to see a PostgreSQL crash course on the channel? What's your opinion about docker vs kubernetes? Let us know!

Docker image source (if the image infridges any copyright or something else is wrong, do let us know via email and it will be removed):

Music:
Barbados by Scandinavianz
Creative Commons — Attribution 3.0 Unported — CC BY 3.0
Free Download / Stream:
Music promoted by Audio Library: