From 2292574c5fd55c74045553df266514aba9fc6e75 Mon Sep 17 00:00:00 2001 From: Josh Earlenbaugh Date: Fri, 16 Aug 2024 15:18:39 -0400 Subject: [PATCH 1/2] Solid draft. --- .../release/getting_started/docker_env.mdx | 176 ++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 product_docs/docs/biganimal/release/getting_started/docker_env.mdx diff --git a/product_docs/docs/biganimal/release/getting_started/docker_env.mdx b/product_docs/docs/biganimal/release/getting_started/docker_env.mdx new file mode 100644 index 0000000000..91850a8688 --- /dev/null +++ b/product_docs/docs/biganimal/release/getting_started/docker_env.mdx @@ -0,0 +1,176 @@ +--- +title: Installing PostgreSQL in a Docker container on your local machine +navTitle: Installing PostgreSQL in a Docker container +description: Learn how to install PostgreSQL in a Docker container on your local machine for development purposes. +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) + +## Preparing Docker + +### Install Docker: + +* macOS: Download and install Docker Desktop from [Docker’s official website](https://docs.docker.com/desktop/install/mac-install/). +* 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. +* Linux: Install Docker using your distribution’s package manager. For example, on Ubuntu: + + ```bash + sudo apt update + sudo apt install docker.io + sudo systemctl start docker + sudo systemctl enable docker + sudo usermod -aG docker $USER + newgrp docker + ``` + + 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. + +### 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: + +```bash +docker pull postgres +``` + +## Running and accessing the container’s PostgreSQL database + +### Run the PostgreSQL Container: + +Run a new container with the PostgreSQL image using the following command: + +```bash +docker run --name my_postgres -d \ + -e POSTGRES_PASSWORD=mysecretpassword \ + -v my_pgdata:/var/lib/postgresql/data \ + -p 5432:5432 postgres +``` + +#### `--name my_postgres -d postgres` + +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. + +#### `-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. + +#### `-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. 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. + +#### `-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. + +### Verify the container is running: + +To verify that the container is running, use the following command: + +```bash +docker ps +``` + +This command lists all running containers. You should see the `my_postgres` container listed. + +You now have a persistent, locally accessible Postgres database running in a Docker container. +Let's start using it. + +### Access PostgreSQL + +To access the PostgreSQL database without any additional tools, you can use the following command to open a PostgreSQL prompt: + +```bash +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. + +#### Using a PostgreSQL client + +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. + +##### macOS: + +You can install the PostgreSQL client using Homebrew: + +```bash +brew install libpq +``` + +##### Windows: + +Download the PostgreSQL client from the [official website](https://www.enterprisedb.com/downloads/postgres-postgresql-downloads). + +##### Linux: + +Use your distribution’s package manager to install the PostgreSQL client. For example, on Ubuntu: + +```bash +sudo apt-get install postgresql-client +``` + +#### Connecting other apps + +You can also connect other applications to the PostgreSQL database running in the Docker container. You need to provide the following connection details: + +* Host: `localhost` +* Port: `5432` +* Username: `postgres` +* Password: (whatever you set it to) +* Database: `postgres` + +Or use the connection string: + +``` + postgresql://postgres:mysecretpassword@localhost:5432/postgres +``` + +### Verify data persistence + +1. **Create a table and insert data. ** + Access the PostgreSQL instance and run the following SQL commands to create a table with columns and insert some data: + + ```sql + CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + first_name VARCHAR(50), + last_name VARCHAR(50), + email VARCHAR(100), + hire_date DATE + ); + INSERT INTO employees (first_name, last_name, email, hire_date) VALUES + ('John', 'Doe','john.doe@example.com', '2020-01-15'), + ('Jane', 'Smith', 'jane.smith@example.com', '2019-03-22'); + +2. **Stop and completely remove the container.** + + ```bash + docker stop my_postgres + docker rm my_postgres + ``` +3. **Recreate the container with the same volume.** + + ```bash + docker run --name my_postgres -d \ + -e POSTGRES_PASSWORD=mysecretpassword \ + -v my_pgdata:/var/lib/postgresql/data \ + -p 5432:5432 postgres + ``` +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. + +## Conclusion + +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. From bb297a18e85a59b6e75937d31ef805c73ec4c0bb Mon Sep 17 00:00:00 2001 From: Josh Earlenbaugh Date: Mon, 19 Aug 2024 15:23:20 -0400 Subject: [PATCH 2/2] Changed procedure to link to procedure. --- .../release/getting_started/docker_env.mdx | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/product_docs/docs/biganimal/release/getting_started/docker_env.mdx b/product_docs/docs/biganimal/release/getting_started/docker_env.mdx index 91850a8688..c81e62e0e1 100644 --- a/product_docs/docs/biganimal/release/getting_started/docker_env.mdx +++ b/product_docs/docs/biganimal/release/getting_started/docker_env.mdx @@ -17,16 +17,7 @@ Using Docker for your local PostgreSQL development environment streamlines setup * macOS: Download and install Docker Desktop from [Docker’s official website](https://docs.docker.com/desktop/install/mac-install/). * 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. -* Linux: Install Docker using your distribution’s package manager. For example, on Ubuntu: - - ```bash - sudo apt update - sudo apt install docker.io - sudo systemctl start docker - sudo systemctl enable docker - sudo usermod -aG docker $USER - newgrp docker - ``` +* Linux: Download and install Docker Desktop from [Docker's official website](https://docs.docker.com/desktop/install/linux-install/). 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. @@ -156,10 +147,10 @@ Or use the connection string: 3. **Recreate the container with the same volume.** ```bash - docker run --name my_postgres -d \ - -e POSTGRES_PASSWORD=mysecretpassword \ - -v my_pgdata:/var/lib/postgresql/data \ - -p 5432:5432 postgres + docker run --name my_postgres -d \ + -e POSTGRES_PASSWORD=mysecretpassword \ + -v my_pgdata:/var/lib/postgresql/data \ + -p 5432:5432 postgres ``` 4. **Verify data persistence.**