![]() |
VOOZH | about |
PostgreSQL, an effective tool, is a free-to-use relational database management system. Docker can quickly construct and orchestrate its instances without bothering about the complexity of setup or dependencies. This step-by-step simple guide will show you how to get PostgreSQL on Docker, and then, use the pgAdmin extension to look at the database.
When it comes to working with PostgreSQL or any other databases, the problem comes when installing and configuring it. As a developer, setting up the database each time can be a real trouble. By entering Docker and by pulling PostgreSQL within a Docker container, you can effortlessly use PostgreSQL Database which makes it much easier to configure and concentrate on the actual coding.
But, a fair warning is that the data inside a containerized PostgreSQL ( which means running a Postgres database inside a container that contains all the resources to run it ) doesn't retain when the container is stopped. To tackle this vanishing act, you can attach a local directory as a volume. This way, the PostgreSQL database data finds a spot in the local volume and then stores the data in the database.
First, you need a Docker on your system. Install Docker from its official website, suiting your operating system.
After the Docker Desktop is started, you get the dashboard.
Type "PostgreSQL" in the search bar. Looking under "Images" on the Docker Desktop dashboard you can find an image on the top. The official PostgreSQL image should come up from Docker Hub.
👁 Finding the PostgreSQL Docker Image
Click on "pull" to get the image. Docker will start pulling the PostgreSQL image to your computer.
Once the image is downloaded, Open command prompt and run the PostgreSQL container using the following command:
docker run --name pg -e POSTGRES_USER=root -e POSTGRES_PASSWORD=pass -p 5432:5432 -d postgres
Explanation:
In Docker Desktop's "Containers/Apps" part, look for the container's status. Once it's active, the "postgres-container" should show a green light. This means it's working fine.
👁 Checking the PostgreSQL Container
Look at the left sidebar. You'll see an option for extensions. This leads you to the extensions on your Docker Desktop. Click here and search for "postgres". You'll find something labeled "Open Source Management Tool For Postgres". Go ahead and install it.
Once installed, it's time to run the pgAdmin extension.
To visualize the PostgreSQL database in pgAdmin, you need to add the PostgreSQL server to pgAdmin.
👁 Connecting With The Postgres Database
👁 Enter password and click on save
You can now use the pgAdmin interface to browse the PostgreSQL database after adding the server. To navigate and manage your databases, tables, and data, use the left sidebar.
Docker lets you handle lots of containers. You can also tackle many projects with different PostgreSQL versions. Just like how pgAdmin's visuals make handling databases easy, using it can also greatly boost your PostgreSQL development work.
Security plays an important role in all aspects. Here securing the postgreSQL database and pgAdmin include some best practices to follow:
Backing up data plays an important role in databases. Here backup and restore of the postgreSQL database can be done using tools such as pg_dump or pg_basebackup:
docker exec -it <container_name> \
pg_dump -U<user_name> --column-inserts --data-only <db_name> > \
backup_data.sql
You might stumble upon these common issues while setting up PostgreSQL & pgAdmin in docker
1. Sometimes connection won't be established between PostgreSQL container and pgAdmin.
Make sure the containers are all on the same Docker network. Check the accuracy of connection parameters such hostname, port, login, and password. Also, ensure that PostgreSQL is set up to allow connections.
docker run --name pg -e POSTGRES_USER=root -e POSTGRES_PASSWORD=pass -p 5432:5432 -d postgres
2. If you are facing issues when trying to execute certain queries or accessing specific tables.
Examine the user permissions in PostgreSQL. Check that the user connecting from pgAdmin has the relevant permissions.
3. A default database will be built and will not allow incoming connections if there isn't one when PostgreSQL in a container starts. This causes issue when connecting with pgAdmin
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 3s
timeout: 3s
retries: 3
Deploying PostgreSQL with pgAdmin using Docker simplifies the entire setup and maintenance of relational databases. It's a game-changer! With pgAdmin, you get a slick visual management tool that takes the user experience to a whole new level. It's like having a personal assistant for your PostgreSQL needs. Now, let me walk you through the exciting steps of unleashing the power of Docker and pgAdmin. We'll start by grabbing those PostgreSQL images like a pro. Then, we'll fire up the containers, ready to conquer the database world. But wait, there's more! We'll dive into configuring pgAdmin extensions, unlocking a treasure trove of database management magic. Say goodbye to tedious tasks