How to Use/Execute PostgreSQL Query in Docker Container

PostgreSQL is a widely used relational DBMS that is designed for efficiently storing, handling, and retrieving large amounts of data in an organized structure. Users can use PostgreSQL in Docker to easily create and manage the PostgreSQL database without installing it on the local host machine. Moreover, users can also use and execute the PostgreSQL queries in a Docker container.

This blog will illustrate the method to use/execute PostgreSQL queries in a Docker container.

How to Use/Execute PostgreSQL Query in Docker Container?

To use/execute PostgreSQL Query in Docker container, try out the below-mentioned steps:

- Download Postgres Docker Image
- Build and Run the Postgres Container
- Interact With Executing Container
- Establish a Connection With a Database Server
- Execute PostgreSQL Queries and psql Commands

Step 1: Download Postgres Docker Image

Execute the below-provided “docker pull” command in Windows PowerShell to download the official Postgres image from the cloud repository (Docker Hub) to your local system:

docker pull postgres
img

Step 2: Build and Run Postgres Container

Create and run the Postgres container via the “docker run --name -d <cont-name> -p 5432:5432 -e POSTGRES_PASSWORD=<password> postgres” command:

docker run -d --name postgresCont -p 5432:5432 -e POSTGRES_PASSWORD=pass123 postgres

Here:

- “-d” flag specifies that the container should execute in the background.
- “--name” option assigns the container’s name, i.e., “postgresCont”.
- “-p” assigns the port for the container i.e. “5432:5432”.
- “-e POSTGRES_PASSWORD” configures the password to be “pass123”.
- “postgres” is the official Docker image:

img

Upon doing so, the Postgres container has been created and started.

Step 3: Interact With Executing Container

Write out the “docker exec -it <cont-name> bash” command and specify the executing Postgres container name to open the shell within it:

docker exec -it postgresCont bash
img

Subsequently, the “PostgresCont” container has been accessed and now we can run commands in it.

Step 4: Establish a Connection With Database Server

Execute the “psql” command along with the hostname and user name to make a connection with the Postgres Database Server:

psql -h localhost -U postgres
img

As you can see, the SQL shell has been started successfully where we can execute/run PostgreSQL queries and psql commands.

Step 5: Execute PostgreSQL Queries and psql Commands

Finally, execute the psql commands in the PostgreSQL container. For instance, we are executing the “\l” command to list available databases:

\l

The below output shows the list of available databases:

img

Execute another psql command, such as the “CREATE DATABASE” command along with the database name to create a database. For instance, we are creating a database named “tsl_employee”:

CREATE DATABASE tsl_employee;

The below output indicates that the database has been created successfully:

img

We have successfully used/executed PostgreSQL queries in a Docker container.

Conclusion

To use/execute PostgreSQL Query in Docker container, first, download the Postgres Docker image. Then, build and run the Postgres container through the “docker run --name -d <cont-name> -p 5432:5432 -e POSTGRES_PASSWORD=<password> postgres” command. Next, interact with the executing container and establish a connection with a database server using the “psql -h localhost -U postgres” command. Finally, execute PostgreSQL queries and psql commands. This blog has illustrated the method to use/execute PostgreSQL queries in a Docker container.