Skip to main content
Hello and welcome back. In this lesson we will run an initialization script to create a few dummy users in our PostgreSQL database. This helps the login microservice have test accounts available during development and QA. Overview
  • Locate the CodeCommit repository in the AWS Console.
  • Clone the repo into your Cloud9 environment (or another terminal).
  • Save and run the helper script that creates the users table and inserts dummy accounts.
  • Verify the inserted rows using a SQL client such as DBeaver.
Open the AWS CodeCommit repository (in my account it’s called login-page-microservice). You should see the same repository in your exercise account.
The image shows a screenshot of the AWS CodeCommit console with a list of repositories, including their names, last modified dates, and options for cloning via HTTPS or SSH.
Prerequisites
  • AWS Cloud9 (or any machine with terminal access to your repo).
  • Python 3 and pip3.
  • psycopg2 installed (or psycopg2-binary).
  • Network access to your PostgreSQL instance (RDS endpoint or other host).
  • A SQL client like DBeaver for verification.
Helper script (save as helper_script.py) Below is a clean, robust helper script that:
  • reads DB connection settings from environment variables,
  • creates the users table if it doesn’t exist,
  • inserts dummy users using ON CONFLICT DO NOTHING to avoid duplicate-key errors.
Environment variables reference Important: use environment variables or a secrets manager for credentials; avoid hardcoding secrets. Clone and run from Cloud9 (or any terminal in the repo)
  1. Clone the CodeCommit repository into your environment (HTTPS example):
You should see output similar to:
  1. If psycopg2 is missing, install dependencies from requirements.txt:
Common error before installing dependencies:
  1. Ensure environment variables are set (example Bash):
  1. Run the helper script:
If the script runs successfully it typically returns to the command prompt without additional output — this indicates the script executed and committed changes to the database.
Always prefer using environment variables for credentials (or a secrets manager). Avoid hardcoding passwords or database hosts directly in scripts.
Verify the inserted rows with DBeaver To confirm the table and data:
  • Open DBeaver and create a new PostgreSQL connection.
  • Provide host, port, username, password, and database name (for example microservice).
  • Expand the connection: Databases → microservice → Schemas → public → Tables to find the users table.
The image shows the DBeaver interface with a PostgreSQL connection settings window open, where database connection details such as server, URL, authentication, and username are being configured.
Open an SQL editor against the microservice database and run:
The image shows a screenshot of DBeaver with a database named "microservice" open, displaying the database navigator and a context menu for SQL editor options.
You should see the three dummy users in the result set. Use one of these accounts to log into the application for testing. Troubleshooting: duplicate-key error If you run a previous version of the script repeatedly without conflict handling, you may encounter a UniqueViolation on the email column. Example:
If you see a unique constraint error, either update the script to upsert or use ON CONFLICT DO NOTHING (as shown above), or clear the table before reinserting data if that is acceptable for your workflow.
Next steps
  • Review how the login microservice queries the users table and integrates authentication.
  • Promote the initialization script into a deployment automation step if you want repeatable environment setups (consider migrations tools such as Flyway or Alembic for production schema changes).
Links and references

Watch Video