🛎️ Discover thousands of remote developer job openings now 👉 -

Check out Devremote

Setup Local Postgres Database Using Docker

Setup Local Postgres Database Using Docker
Photo by Mohammad Rahmani / Unsplash

PostgreSQL is a powerful, open-source object-relational database management system (ORDBMS) widely used for web, mobile, geospatial, and analytics applications. It is known for its robust features, including support for complex queries, foreign keys, and triggers, and its ability to handle large volumes of data.

Docker is a containerization platform that allows you to package applications and their dependencies into lightweight containers that can be easily deployed and run on any host machine. This makes it easy to develop, test, and deploy applications quickly and consistently, regardless of the underlying operating system or infrastructure.

Using Docker to run PostgreSQL, you can easily create and manage multiple PostgreSQL instances on a single host machine and quickly deploy and run PostgreSQL in different environments. This can be particularly useful for development and testing. It allows you to easily create and destroy PostgreSQL instances as needed without worrying about setting up and configuring a separate PostgreSQL installation for each instance.

How To Setup Docker and Postgres on Windows

First, make sure that you have Docker installed on your machine. If you haven't installed Docker, download it from the Docker website (https://www.docker.com/).

Once Docker is installed, open a command prompt window and run the following command to pull the latest version of the PostgreSQL Docker image:

docker pull postgres

Next, run the following command to create a new Docker container based on the PostgreSQL image:

docker run --name my-postgres -e POSTGRES_PASSWORD=password -d postgres

This command will create a new Docker container named "my-postgres" and set the password for the default PostgreSQL user ("postgres") to "password".

To connect to the PostgreSQL database from the command prompt, you will need to use the psql command-line tool. If you don't have psql installed, you can download it from the PostgreSQL website (https://www.postgresql.org/).

Once psql is installed, run the following command to connect to the PostgreSQL database:

psql -h localhost -U postgres

This will connect to the PostgreSQL database running in the Docker container and open a psql command prompt.

To create a new database, run the following command at the psql prompt:

CREATE DATABASE mydatabase;

This will create a new database named "mydatabase".

To exit the psql prompt, type "\q" and press Enter.

That's it! It would help if you now had a PostgreSQL database running in a Docker container on your local machine. You can use psql or any other PostgreSQL client tool to connect to the database and start working with it.

How To Setup Docker and Postgres on MacOS

Setting up Postgres with Docker on mac is more or less the same as above. There are, however, a few differences, as you will see below.  

Install Docker, open a terminal window and run the following command to pull the latest version of the PostgreSQL Docker image:

docker pull postgres

Next, run the following command to create a new Docker container based on the PostgreSQL image:

docker run --name my-postgres -e POSTGRES_PASSWORD=password -d postgres

This command will create a new Docker container named "my-postgres" and set the password for the default PostgreSQL user ("postgres") to "password".

To connect to the PostgreSQL database from the terminal, you will need to use the psql command-line tool. If you don't have psql installed, you can install it using Homebrew by running the following command:

brew install postgresql

Once psql is installed, run the following command to connect to the PostgreSQL database:

psql -h localhost -U postgres

This will connect to the PostgreSQL database running in the Docker container and open a psql command prompt.

To create a new database, run the following command at the psql prompt:

CREATE DATABASE mydatabase;

This will create a new database named "mydatabase". To exit the psql prompt, type "\q" and press Enter.

Conclusion

Docker and PostgreSQL can be a powerful combination for developing, testing, and deploying applications. By using Docker to run PostgreSQL, you can easily create and manage multiple PostgreSQL instances on a single host machine and quickly deploy and run PostgreSQL in different environments.

This can be particularly useful for development and testing. It allows you to easily create and destroy PostgreSQL instances as needed without worrying about setting up and configuring a separate PostgreSQL installation for each instance. To set up PostgreSQL using Docker on Windows or macOS, you will need to install Docker, pull the latest version of the PostgreSQL Docker image, create a new Docker container based on the image, install the psql command-line tool, connect to the PostgreSQL database, and create a new database. Once you've completed these steps, you'll be ready to start working with PostgreSQL in Docker.