Installing PostgreSQL with Docker
Tags: postgres, docker, data, newbie
Published: 2024-06-23 11:37:01

Introduction
------------

In this guide, I'm going to walk you through installing PostgreSQL database and **pgAdmin** using Docker. The big advantage here is it's quick and straightforward. You won't need to go through a long manual installation process (and potentially spend time fixing errors if they arise).

Installing Docker
-----------------

If you don't already have Docker installed on your machine, you'll need to do that first. At this step, you'll need to search Google because the installation process depends on the operating system you're using. Typically, installing **Docker** on **Windows** is simpler compared to using **Ubuntu**.

  

Installing PostgreSQL
---------------------

Once Docker is set up, the next step is to install the **PostgreSQL** image. Here, I'm using **postgres:alpine**, which is a minimal version of PostgreSQL (it's lightweight and includes all the essential components needed to use PostgreSQL).

```sh
docker run --name postgresql -e POSTGRES_USER={username} -e POSTGRES_PASSWORD={password} -p 5432:5432 -v {directory}:/var/lib/postgresql/data -d postgres:alpine
```

Next, replace the **username** and **password** with the credentials you want to use. For the **directory** part, choose a directory on your local machine where you want to store the PostgreSQL data.

  

Now, let's proceed with installing **pgAdmin4**:

```sh
docker run -p {port}:80 -e PGADMIN_DEFAULT_EMAIL={email} -e PGADMIN_DEFAULT_PASSWORD={password} -d dpage/pgadmin4
```
Also, replace the **email**, **password**, and **port** with the ones you want to use.

To access **pgAdmin**, open your browser and go to **_http://0.0.0.0:8900_**, where **8900** is the port set in the docker run command for **pgAdmin4** above.

Here's what you'll see:

![Admin page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gug4eanv9swm8jw2638t.png)

Then use the email and password you set up to login.

  

Using pgAdmin4
--------------

### Basic Concepts

In PostgreSQL, you can create multiple **Server Groups**, each containing multiple **Servers**, and each **Server** can have multiple **Databases**.

  

### Creating Servers and Databases

To create a new Server, follow these steps: right-click on **Server Group** > **Register** > **Server...**

Next, in the **General** tab, enter the **Name** as the server name.


![Register - Server General](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4k6cxsqi2ofdicocbab.png)


In the **Connection** tab, enter the following information:


![Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j55yxx4r2d84ggd2j2th.png)


*   **Host name/address**: You can find this information by inspecting the PostgreSQL container to get the IP address using the following command: "**_docker inspect {container id}_**"
*   **Maintenance database** is the database name.
*   **Port**, **Username**, and **Password** are the information you used when running the docker postgres.

After clicking Save, a new **Database** will be created.

  

### Basic Query

In this step, I'll provide you with some simple queries to check if **PostgreSQL** is working or not. PostgreSQL queries are quite similar to **SQL**, so if you have basic SQL knowledge, it won't be difficult to understand.

Open the **Query Tool** and execute the following **SQL** code:

```sql
-- create table
CREATE TABLE person (
  id int PRIMARY KEY,
  name text,
  age int,
  address text,
);

-- insert data
INSERT INTO person values
(1, 'name 1', 21, 'address 1'),
(2, 'name 2', 22, 'address 2');

-- query data
SELECT * FROM person;
SELECT name, address FROM person;
```

![Table created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldijnqj9vglet8fr0q74.png)

If you get query results like these, then you've successfully completed the setup:

![Query successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/584joqyye2euub7cvwn8.png)

Conclusion
----------

In this article, I've guided you on using **Docker** to install and run **PostgreSQL**, using **pgAdmin** as the interface to perform visual operations with the database. Additionally, I provided some simple queries to get you started with **Postgres**.

**_If you found this content helpful, please visit [the original article on my blog](https://howtodevez.blogspot.com/2024/03/installing-postgresql-with-docker.html) to support the author and explore more interesting content._**

<a href="https://howtodevez.blogspot.com/2024/03/sitemap.html" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/Blogger-FF5722?style=for-the-badge&logo=blogger&logoColor=white" width="36" height="36" alt="Blogspot" /></a><a href="https://dev.to/chauhoangminhnguyen" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/dev.to-0A0A0A?style=for-the-badge&logo=dev.to&logoColor=white" width="36" height="36" alt="Dev.to" /></a><a href="https://www.facebook.com/profile.php?id=61557154776384" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/Facebook-1877F2?style=for-the-badge&logo=facebook&logoColor=white" width="36" height="36" alt="Facebook" /></a><a href="https://x.com/DavidNguyenSE" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/X-000000?style=for-the-badge&logo=x&logoColor=white" width="36" height="36" alt="X" /></a>