# 1.2.0 Postgres and DynamoDB

## **0. Learning Materials**

* Video: [Week 1 - DynamoDB and Postgres vs Docker](https://youtu.be/CbQNMaa6zTg?list=PLBfufR7vyJJ7k25byhRXJldB5AiwgNnWv)
* Andrew's repo: [week-1-add-postgres-dynamodb](https://github.com/omenking/aws-bootcamp-cruddur-2023/tree/week-1-add-postgres-dynamodb)
* My branch repo: [01-02-dynamodb-postgres](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/tree/01-02-dynamodb-postgres)
  * My commits
    * [#5](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/issues/5) [Added dynamodb and postgres containers](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/commit/ad4d2bef01e44c141a1aea8631e3d191d4129f53)

### Task List

* [ ] Install and containerise Postgres
* [ ] Install and containerise DynamoDB

***

## 1. Workflow

### 1.1 Build containers

Add the following docker container configurations for:

* `dynamodb-local`
* `db` (postgresql)

{% code title="docker-compose.yml" lineNumbers="true" %}

```yaml
  ...
  dynamodb-local:
    # https://stackoverflow.com/questions/67533058/persist-local-dynamodb-data-in-volumes-lack-permission-unable-to-open-databa
    # We needed to add user:root to get this working.
    user: root
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal
  db:
    image: postgres:13-alpine
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    ports:
      - '5432:5432'
    volumes:
      - db:/var/lib/postgresql/data
  
  ...
  
volumes:
  db:
    driver: local
```

{% endcode %}

Then run `docker-compose`.

* `docker compose -f 'docker-compose.yml' up -d --build`

<div data-full-width="true"><figure><img src="/files/I6FBlCIK6YNN9aYvryFO" alt=""><figcaption><p>docker compose busy pulling and building our containers. </p></figcaption></figure></div>

<figure><img src="/files/yKV15j5gxXmpmXLId8Vg" alt=""><figcaption></figcaption></figure>

### 1.2 Test the database

In case you don't have `aws-cli` or `postgres` installed in your local machine, see [discussion](#2.1-install-dynamodb-and-postgresql).

Now that the DynamoDB container is running locally, create a table.&#x20;

* Command [Source code](https://github.com/100DaysOfCloud/challenge-dynamodb-local) @ [100DaysOfCloud](https://github.com/100DaysOfCloud)

```bash
aws dynamodb create-table \
    --endpoint-url http://localhost:8000 \
    --table-name Music \
    --attribute-definitions \
        AttributeName=Artist,AttributeType=S \
        AttributeName=SongTitle,AttributeType=S \
    --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \
    --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
    --table-class STANDARD
```

<figure><img src="/files/81YJCAFUn3N8XJpHbQ5c" alt=""><figcaption></figcaption></figure>

Once the table is created, populate the db with some data.&#x20;

{% code fullWidth="true" %}

```bash
aws dynamodb put-item \
    --endpoint-url http://localhost:8000 \
    --table-name Music \
    --item \
        '{"Artist": {"S": "No One You Know"}, 
          "SongTitle": {"S": "Call Me Today"}, 
          "AlbumTitle": {"S": "Somewhat Famous"}}' \
    --return-consumed-capacity TOTAL  
```

{% endcode %}

<figure><img src="/files/AbAAm9Tz09fMihXj22eQ" alt=""><figcaption></figcaption></figure>

Test the connection from our local machine to the locally running Postgres database.

<div data-full-width="true"><figure><img src="/files/PfG1rCQW4MiN1bHpptdy" alt=""><figcaption><p>This db testing extension is called "vscode-database-client2" by cweijan. You can see that the container is running and the port is open (5432)</p></figcaption></figure></div>

You can also connect to the postgres db with the command line:

* `psql -Upostgres --host localhost`

<div data-full-width="true"><figure><img src="/files/KJP1o7KnGIyRqdMNaAAL" alt=""><figcaption><p>The connection was successful.</p></figcaption></figure></div>

## 2. Discussion

### 2.1 Install DynamoDB and PostgreSQL

* DynamoDB

```bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
```

* PostgreSQL

```bash
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee  /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt install -y postgresql-client-13 libpq-dev
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gwen-leigh.gitbook.io/free-aws-cloud-project-bookcamp/week-1-dockerise-app/1.2.0-postgres-and-dynamodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
