Skip to content

Commit 843ea5a

Browse files
committed
Solid draft.
1 parent f06ef98 commit 843ea5a

1 file changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
title: Installing PostgreSQL in a Docker container on your local machine
3+
navTitle: Installing PostgreSQL in a Docker container
4+
description: Learn how to install PostgreSQL in a Docker container on your local machine for development purposes.
5+
deepToC: true
6+
---
7+
8+
Using Docker for your local PostgreSQL development environment streamlines setup, ensures consistency, and simplifies management. It provides a flexible, isolated, and portable solution that can adapt to various development needs and workflows.
9+
10+
## Prerequisites
11+
12+
* Docker-compatible OS (macOS, Windows, Linux)
13+
14+
## Preparing Docker
15+
16+
### Install Docker:
17+
18+
* macOS: Download and install Docker Desktop from [Docker’s official website](https://docs.docker.com/desktop/install/mac-install/).
19+
* Windows: Download and install Docker Desktop from [Docker’s official website](https://docs.docker.com/desktop/install/windows-install/). Ensure WSL 2 is enabled if using Windows 10 or later.
20+
* Linux: Install Docker using your distribution’s package manager. For example, on Ubuntu:
21+
22+
```bash
23+
sudo apt update
24+
sudo apt install docker.io
25+
sudo systemctl start docker
26+
sudo systemctl enable docker
27+
sudo usermod -aG docker $USER
28+
newgrp docker
29+
```
30+
31+
This sequence of commands updates the package lists, installs Docker, starts and enables Docker to run at boot, adds your user to the Docker group, and then immediately applies the group changes.
32+
33+
### Pull the PostgreSQL Docker image:
34+
35+
Open a terminal or command prompt and run the following command to pull the latest PostgreSQL image from Docker Hub:
36+
37+
```bash
38+
docker pull postgres
39+
```
40+
41+
## Running and accessing the container’s PostgreSQL database
42+
43+
### Run the PostgreSQL Container:
44+
45+
Run a new container with the PostgreSQL image using the following command:
46+
47+
```bash
48+
docker run --name my_postgres -d \
49+
-e POSTGRES_PASSWORD=mysecretpassword \
50+
-v my_pgdata:/var/lib/postgresql/data \
51+
-p 5432:5432 postgres
52+
```
53+
54+
#### `--name my_postgres -d postgres`
55+
56+
The `--name` flag tells docker to creates a new container named `my_postgres`, while the `-d` flag tells it to use the `postgres` image which we pulled previously. Note that if we had not pulled it, this command would automatically pull the PostgreSQL image.
57+
58+
#### `-e POSTGRES_PASSWORD=mysecretpassword`
59+
60+
The `-e` flag sets an environment variable `POSTGRES_PASSWORD` to `mysecretpassword`. This is used the password for the default `postgres` user. You should use a different password.
61+
62+
#### `-v my_pgdata:/var/lib/postgresql/data`
63+
64+
Docker uses volumes to persist data in Docker containers. This flag mounts a volume named `my_pgdata` to persist data. The data in this case is whatever what Postgres writes to the `/var/lib/postgresql/data` directory within the container. These writes are persisted outside the container in a docker volume; the command `docker volume inspect my_pgdata` will show you information about that volume.
65+
66+
#### `-p 5432:5432`
67+
68+
The `-p` flag maps the container’s port 5432 to the host machine’s port 5432. Port 5432 is Postgres's default port for communications. By using this flag, it allows you to access the PostgreSQL database from your host machine.
69+
70+
### Verify the container is running:
71+
72+
To verify that the container is running, use the following command:
73+
74+
```bash
75+
docker ps
76+
```
77+
78+
This command lists all running containers. You should see the `my_postgres` container listed.
79+
80+
You now have a persistent, locally accessible Postgres database running in a Docker container.
81+
Let's start using it.
82+
83+
### Access PostgreSQL
84+
85+
To access the PostgreSQL database without any additional tools, you can use the following command to open a PostgreSQL prompt:
86+
87+
```bash
88+
docker exec \-it my\_postgres psql \-U postgres
89+
```
90+
91+
This logs into the Docker container and runs the `psql` command as the `postgres` user from there.
92+
93+
#### Using a PostgreSQL client
94+
95+
The `psql` command is a powerful tool for interacting with PostgreSQL databases. You should install it on your local machine to interact with the PostgreSQL database running in the Docker container.
96+
97+
##### macOS:
98+
99+
You can install the PostgreSQL client using Homebrew:
100+
101+
```bash
102+
brew install libpq
103+
```
104+
105+
##### Windows:
106+
107+
Download the PostgreSQL client from the [official website](https://www.enterprisedb.com/downloads/postgres-postgresql-downloads).
108+
109+
##### Linux:
110+
111+
Use your distribution’s package manager to install the PostgreSQL client. For example, on Ubuntu:
112+
113+
```bash
114+
sudo apt-get install postgresql-client
115+
```
116+
117+
#### Connecting other apps
118+
119+
You can also connect other applications to the PostgreSQL database running in the Docker container. You need to provide the following connection details:
120+
121+
* Host: `localhost`
122+
* Port: `5432`
123+
* Username: `postgres`
124+
* Password: (whatever you set it to)
125+
* Database: `postgres`
126+
127+
Or use the connection string:
128+
129+
```
130+
postgresql://postgres:mysecretpassword@localhost:5432/postgres
131+
```
132+
133+
### Verify data persistence
134+
135+
1. **Create a table and insert data. **
136+
Access the PostgreSQL instance and run the following SQL commands to create a table with columns and insert some data:
137+
138+
```sql
139+
CREATE TABLE employees (
140+
id SERIAL PRIMARY KEY,
141+
first_name VARCHAR(50),
142+
last_name VARCHAR(50),
143+
email VARCHAR(100),
144+
hire_date DATE
145+
);
146+
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES
147+
('John', 'Doe','john.doe@example.com', '2020-01-15'),
148+
('Jane', 'Smith', 'jane.smith@example.com', '2019-03-22');
149+
150+
2. **Stop and completely remove the container.**
151+
152+
```bash
153+
docker stop my_postgres
154+
docker rm my_postgres
155+
```
156+
3. **Recreate the container with the same volume.**
157+
158+
```bash
159+
docker run --name my_postgres -d \
160+
-e POSTGRES_PASSWORD=mysecretpassword \
161+
-v my_pgdata:/var/lib/postgresql/data \
162+
-p 5432:5432 postgres
163+
```
164+
4. **Verify data persistence.**
165+
166+
Access the PostgreSQL instance and check if the data still exists:
167+
168+
```sql
169+
SELECT * FROM employees
170+
```
171+
172+
If everything worked as expected, you should see the employee table with the data previously loaded still present.
173+
174+
## Conclusion
175+
176+
By following these steps, you have set up a robust local development environment for PostgreSQL using Docker. This setup ensures data persistence and provides a flexible, isolated, and consistent environment for all of your development needs.

0 commit comments

Comments
 (0)