This repository contains a Docker Compose configuration to help you quickly set up a PostgreSQL database and pgAdmin web interface for easy database management.
Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications using containerization technology. Containers are lightweight, isolated environments that package applications and their dependencies, ensuring consistent execution across various environments.
Before getting started, ensure you have the following prerequisites installed on your system:
-
Docker: Install Docker on your machine by following the instructions for your specific operating system:
Follow these steps to set up PostgreSQL and pgAdmin using Docker Compose:
-
Clone this repository to your local machine or download the
docker-compose.yml
file from this repository. -
Open a terminal or command prompt, navigate to the directory containing the
docker-compose.yml
file. -
Run the following command to start the PostgreSQL and pgAdmin containers:
docker-compose up -d
-
Docker Compose will download the required Docker images and start the containers in the background.
-
Once the containers are up and running, you can access the following services:
- PostgreSQL:
localhost:5433
(In this case we will access PostgreSQL via pgAdmin) - pgAdmin:
localhost:5050
- PostgreSQL:
-
Access the pgAdmin web interface by navigating to
localhost:5050
in your web browser. Log in using the following credentials:- Email:
[email protected]
- Password:
root
- Email:
-
In the pgAdmin interface, you can add a new server and manage your PostgreSQL database effortlessly.
To verify that the installation has completed successfully and PostgreSQL is running:
-
Open pgAdmin in your web browser (
localhost:5050
). -
On the left-hand side of the pgAdmin interface, right-click on the "Servers" option under the "Browser" section.
-
Select "Register -> Server" from the context menu.
-
In the "General" tab, provide a name for your server (e.g., My PostgreSQL Server).
-
Switch to the "Connection" tab and enter the following details:
- Host name/address:
db
- Port:
5432
- Username:
admin
- Password:
root
- Host name/address:
-
Click the "Save" button to add the server.
-
In the pgAdmin interface, navigate to
Server -> db -> Database -> test_db
. Right-click on thetest_db
database and select "Query Tool" to open a new tab. -
Copy and paste the following SQL query into the Query Tool tab:
CREATE TABLE accounts (
user_id serial PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);
-
Execute the query by clicking the "Execute" button or pressing
F5
. -
To ensure that the table was created successfully, navigate to
Server -> db -> Database -> test_db -> Schemas -> public -> Tables
. You should see theaccounts
table listed.
If you can see the accounts
table, congratulations! You have successfully completed the installation and configuration of PostgreSQL and pgAdmin using Docker Compose.
If you do not see the accounts
table or encounter any issues, try the following steps:
-
Right-click on the "Servers" option under the "Browser" section in pgAdmin and select "Refresh." This action may update the list of databases and tables.
-
Double-check the SQL query you copied into the Query Tool and ensure there are no typos or syntax errors.
-
If the issue persists, consider stopping and removing the containers using
docker-compose down
, and then try the installation process again.
To stop and remove the containers while keeping the data volumes intact, use the following command:
docker-compose down
If you also want to remove the data volumes along with the containers, use the --volumes
flag:
docker-compose down --volumes
Note: Be cautious when using the --volumes
flag, as it will delete all data stored in PostgreSQL and pgAdmin.
Congratulations! You have successfully set up PostgreSQL and pgAdmin using Docker Compose. Now you can manage your databases with ease through the pgAdmin web interface. Docker's containerization technology provides a consistent and efficient way to develop, deploy, and manage applications across different environments.
Happy coding!