DEV Community

Cover image for Installing PostgreSQL with Docker
chauhoangminhnguyen
chauhoangminhnguyen

Posted on • Edited on • Originally published at howtodevez.blogspot.com

Installing PostgreSQL with Docker

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).

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

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:

docker run -p {port}:80 -e PGADMIN_DEFAULT_EMAIL={email} -e PGADMIN_DEFAULT_PASSWORD={password} -d dpage/pgadmin4
Enter fullscreen mode Exit fullscreen mode

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

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

In the Connection tab, enter the following information:

Connection

  • 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:

-- 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;
Enter fullscreen mode Exit fullscreen mode

Table created

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

Query successful

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 to support the author and explore more interesting content.

BlogspotDev.toFacebookX

Top comments (0)