Page cover image

1.2.0 Postgres and DynamoDB

0. Learning Materials

Task List


1. Workflow

1.1 Build containers

Add the following docker container configurations for:

  • dynamodb-local

  • db (postgresql)

docker-compose.yml
  ...
  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

Then run docker-compose.

  • docker compose -f 'docker-compose.yml' up -d --build

docker compose busy pulling and building our containers.

1.2 Test the database

In case you don't have aws-cli or postgres installed in your local machine, see discussion.

Now that the DynamoDB container is running locally, create a table.

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

Once the table is created, populate the db with some data.

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  

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

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)

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

  • psql -Upostgres --host localhost

The connection was successful.

2. Discussion

2.1 Install DynamoDB and PostgreSQL

  • DynamoDB

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

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

Last updated