Skip to content

Commit

Permalink
DevGuide POC
Browse files Browse the repository at this point in the history
Signed-off-by: Dj Walker-Morgan <[email protected]>
  • Loading branch information
djw-m committed Aug 13, 2024
1 parent 68ca098 commit 73a8ec6
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 3 deletions.
174 changes: 174 additions & 0 deletions advocacy_docs/dev-guides/deploy/docker.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
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
---

## 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:

* 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.
* Linux: Install Docker using your distribution’s package manager. For example, on Ubuntu:

```
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -ag docker $USER
newgrp 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/).

### 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:

```
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:

```
docker run --name my_postgres -d postgres -e POSTGRES_PASSWORD=mysecretpassword -v my_pgdata:/var/lib/postgresql/data -p 5432:5432
```

#### `--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`

Volumes are used to persist data in Docker containers. This flag mounts a volume named `my_pgdata` to persist data. The data is stored in the `/var/lib/postgresql/data` directory within the container.

#### `-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:

```
docker ps
```

This command lists all running containers. You should see the `my_postgres` container listed.

You now have a persistant, 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:

```
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.

TBD: Installing the psql client on your local machine.

### 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:

```
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:

```
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
```

### Verifying 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','[email protected]', '2020-01-15'),
('Jane', 'Smith', '[email protected]', '2019-03-22');
```

2. Stop and completly remove the container.
```
docker stop my_postgres
docker rm my_postgres
```

3. Recreate 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.
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.
7 changes: 7 additions & 0 deletions advocacy_docs/dev-guides/deploy/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Deploying Postgres for developers
navTitle: Deploying for developers
description: How to deploy Postgres for developers.
---


6 changes: 6 additions & 0 deletions advocacy_docs/dev-guides/developing/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Developing applications with Postgres
navTitle: Developing applications
description: How to develop applications that use Postgres databases.
---

32 changes: 32 additions & 0 deletions advocacy_docs/dev-guides/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: The EDB Postgres AI Developer Guides
navTitle: Developer Guides
description: The EDB Postgres AI Developer Guides provide information on how to use the EDB Postgres AI platform to build and develop Postgres and AI applications.
deepToC: true
directoryDefaults:
iconName: "CodeWriting"
indexCards: simple
prevNext: true
navigation:
- deploy
- working
- developing
---

The EDB Postgres AI Developer Guides are all about providing you, the developer, with the information you need to accelerate your development efforts using the EDB Postgres AI platform. The guides cover a wide range of topics, from setting up your development environment to deploying Postgres and AI applications.

## Deploying Postgres Locally for developers

* [Deploying Postgres Using Docker Locally](deploying/deploying-postgres-docker-locally)

Check failure on line 20 in advocacy_docs/dev-guides/index.mdx

View workflow job for this annotation

GitHub Actions / check-links

pathCheck

invalid URL path: deploying/deploying-postgres-docker-locally (/dev-guides/deploying/deploying-postgres-docker-locally)

## Working with Postgres

* [PSQL for busy developers](working/psql-for-busy-developers)

Check failure on line 24 in advocacy_docs/dev-guides/index.mdx

View workflow job for this annotation

GitHub Actions / check-links

pathCheck

invalid URL path: working/psql-for-busy-developers (/dev-guides/working/psql-for-busy-developers)

## Developing Postgres Applications

* [Developing Postgres Applications with Python](developing/developing-postgres-applications-with-python)

Check failure on line 28 in advocacy_docs/dev-guides/index.mdx

View workflow job for this annotation

GitHub Actions / check-links

pathCheck

invalid URL path: developing/developing-postgres-applications-with-python (/dev-guides/developing/developing-postgres-applications-with-python)




6 changes: 6 additions & 0 deletions advocacy_docs/dev-guides/working/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Working with Postgres tools - guides for developers
navTitle: Working with tools
description: How to work with a range of Postgres tools, with a focus on developers and debugging.
---

14 changes: 11 additions & 3 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ const BannerWideCard = ({ iconName, headingText, to, children }) => (
</div>
);

const BannerWideCardLink = ({ to, className, children }) => (
const BannerWideCardLink = ({ to, className, iconName, children }) => (
<Link
icon={iconName}
to={to}
className={`col-12 col-md-4 py-2 px-5 text-center ${className}`}
style={{ minwidth: "14em" }}
Expand Down Expand Up @@ -276,12 +277,19 @@ const Page = () => {

<BannerWideCard>
<BannerWideCardLink
className="col-md-12"
className="col-md-6"
to="/repos/"
iconName={iconNames.Download}
// iconName={iconNames.DOWNLOAD}
>
Downloads and Repositories
</BannerWideCardLink>
<BannerWideCardLink
className="col-md-6"
to="/dev-guides/"
// iconName={iconNames.CODE_WRITING}
>
Developer Guides
</BannerWideCardLink>
</BannerWideCard>

<div className="row mb-4" data-masonry='{"percentPosition": true }'>
Expand Down

0 comments on commit 73a8ec6

Please sign in to comment.