-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edits to PostgreSQL on Windows dev env topic #6063
Edited all this doc while I was i there.
- Loading branch information
1 parent
b163f98
commit e028e07
Showing
4 changed files
with
81 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,20 @@ description: Learn how to install PostgreSQL in a Docker container on your local | |
deepToC: true | ||
--- | ||
|
||
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. | ||
|
||
## Prerequisites | ||
|
||
* Docker-compatible OS (macOS, Windows, Linux) | ||
|
||
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. | ||
|
||
## Preparing Docker | ||
|
||
### Install Docker: | ||
### Install Docker | ||
|
||
Make sure Docker is installed on your machine or download and install it from [Docker’s official website](https://www.docker.com/products/docker-desktop/). | ||
Make sure Docker is installed on your machine, or download and install it from [Docker’s official website](https://www.docker.com/products/docker-desktop/). | ||
|
||
* macOS: Download and install Docker Desktop from Docker’s official website. | ||
* Windows: Download and install Docker Desktop from Docker’s official website. Ensure WSL 2 is enabled if using Windows 10 or later. | ||
* Windows: Download and install Docker Desktop from Docker’s official website. Ensure WSL 2 is enabled if using Windows 10 or later. | ||
* Linux: Install Docker using your distribution’s package manager. For example, on Ubuntu: | ||
|
||
``` | ||
|
@@ -31,71 +31,71 @@ newgrp docker | |
``` | ||
|
||
|
||
### Pull the PostgreSQL Docker image: | ||
### Pull the PostgreSQL Docker image | ||
|
||
Open a terminal or command prompt and run the following command to pull the latest PostgreSQL image from Docker Hub: | ||
To pull the latest PostgreSQL image from Docker Hub, open a terminal or command prompt and run the following command: | ||
|
||
``` | ||
docker pull postgres | ||
``` | ||
|
||
## Running the PostgreSQL container | ||
|
||
Run a new container with the PostgreSQL image using the following command: | ||
Run a new container with the PostgreSQL image: | ||
|
||
``` | ||
docker run --name my_postgres -d postgres -e POSTGRES_PASSWORD=mysecretpassword -v my_pgdata:/var/lib/postgresql/data -p 5432:5432 | ||
``` | ||
|
||
#### `--name my_postgres` | ||
|
||
The `--name` flag tells docker to creates a new container named `my_postgres`. | ||
The `--name` flag tells Docker to create a new container named `my_postgres`. | ||
|
||
#### `-d` | ||
|
||
The `-d` flag tells Docker to run the container in detached mode. This means the container runs in the background and does not block the terminal. | ||
The `-d` flag tells Docker to run the container in detached mode. This means the container runs in the background and doesn't block the terminal. | ||
|
||
#### `postgres` | ||
|
||
This is the name of the image to run. Docker uses this name to pull the image from Docker Hub if it is not already present on the local machine. Note that if we had not pulled it, this command would automatically pull the PostgreSQL image. | ||
This is the name of the image to run. Docker uses this name to pull the image from Docker Hub if it isn't already present on the local machine. If you don't pull it, this command automatically pulls the PostgreSQL image. | ||
|
||
#### `-e POSTGRES_PASSWORD=mysecretpassword` | ||
|
||
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. | ||
The `-e` flag sets an environment variable `POSTGRES_PASSWORD` to `mysecretpassword`. This is the password for the default postgres user. We strongly recommend using a different password. | ||
|
||
#### `-v my_pgdata:/var/lib/postgresql/data` | ||
Docker uses volumes to persist data in Docker containers. This flag mounts a volume named `my_pgdata` to persist data. | ||
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 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. | ||
These writes are persisted outside the container in a Docker volume. The command `docker volume inspect my_pgdata` shows you information about that volume. | ||
|
||
#### `-p 5432:5432` | ||
|
||
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. | ||
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. Using this flag allows you to access the PostgreSQL database from your host machine. | ||
|
||
## Verifying the container is running | ||
|
||
To verify that the container is running, use the following command: | ||
Verify that the container is running: | ||
|
||
``` | ||
docker ps | ||
``` | ||
|
||
This command lists all running containers. You should see the `my_postgres` container listed. | ||
This command lists all running containers, including the container you created (`my_postgres` in this example). | ||
|
||
You now have a persistent, locally accessible Postgres database running in a Docker container. | ||
You can now start using it. | ||
You now have a persistent, locally accessible Postgres database running in a Docker container and | ||
can start using it. | ||
|
||
## Access PostgreSQL with a client | ||
|
||
To access the PostgreSQL database, without any additional tools, you can use the following command to open a PostgreSQL prompt: | ||
To access the PostgreSQL database without any additional tools, you can use the following command to open a PostgreSQL prompt: | ||
|
||
``` | ||
docker exec \-it my\_postgres psql \-U postgres | ||
``` | ||
|
||
This logs into the Docker container and runs the `psql` command as the `postgres` user from there. | ||
This command logs into the Docker container and runs the `psql` command as the postgres user from there. | ||
|
||
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. | ||
The `psql` command is a powerful tool for interacting with PostgreSQL databases. Install it on your local machine to interact with the PostgreSQL database running in the Docker container. | ||
|
||
### macOS | ||
|
||
|
@@ -151,31 +151,31 @@ postgresql://postgres:mysecretpassword@localhost:5432/postgres | |
('Jane', 'Smith', '[email protected]', '2019-03-22'); | ||
``` | ||
|
||
2. Stop and completely remove the container. | ||
2. Stop and completely remove the container: | ||
``` | ||
docker stop my_postgres | ||
docker rm my_postgres | ||
``` | ||
|
||
3. Recreate the container with the same volume. | ||
3. Re-create the container with the same volume: | ||
|
||
``` | ||
docker run --name my_postgres -d postgres -e POSTGRES_PASSWORD=mysecretpassword -v my_pgdata:/var/lib/postgresql/data -p 5432:5432 | ||
``` | ||
|
||
4. Verify Data Persistence. | ||
4. Verify data persistence. | ||
Access the PostgreSQL instance and check if the data still exists: | ||
|
||
```sql | ||
SELECT * FROM employees | ||
``` | ||
|
||
If everything worked as expected, you should see the employee table with the data previously loaded still present. | ||
If everything worked as expected, you'll see the employee table with the data previously loaded still present. | ||
|
||
|
||
## Stopping and removing the container | ||
|
||
To stop and remove the container, use the following commands: | ||
To stop and remove the container: | ||
|
||
``` | ||
docker stop my_postgres | ||
|
@@ -184,7 +184,7 @@ docker rm my_postgres | |
|
||
## Deleting the volume | ||
|
||
To remove the volume containing the database, use the following command (after stopping and removing the container): | ||
To remove the volume containing the database, after stopping and removing the container, use the following command: | ||
|
||
``` | ||
docker volume rm my_pgdata | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.