Now that the rds connects with EC2, it’s time to populate the database.

Create the Database and User in RDS

Before importing data, create the necessary database and user on RDS.

if you are within the database just use -CREATE DATABASE grocerymate_db;

psql -h <your-rds-endpoint> -U postgres -c "CREATE DATABASE grocerymate_db;" #If this is not already created when you did the RDS
psql -h <your-rds-endpoint> -U postgres -c "CREATE USER grocery_user WITH ENCRYPTED PASSWORD 'grocery_test';"

Grant Permissions:

GRANT USAGE, CREATE ON SCHEMA public TO grocery_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO grocery_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO grocery_user;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO grocery_user;

-- Set default privileges for future objects
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO grocery_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO grocery_user;

Verify User and Schema Permissions:

\\dn+ public # Should show grocery_user=UC/pg_database_owner
\\ddp # Should show grocery_user with table/sequence privileges

🔹 Explanation:

After connecting to your database, make sure to exit the database with Ctrl-C to be able to prompt commands to your EC2 instance again instead of your database before you go on with the next step.

Populate RDS with Application Data

Since the RDS database is initially empty, we need to populate it with predefined application data from a SQL file. This file contains the necessary schema (tables, columns, constraints) and sample data for products, users, and orders.

✅ Load Initial Data into RDS

Run the following command on EC2 to execute the SQL file and populate the database:

psql -h <your-rds-endpoint>  -U <username> -d <db_name> -f backend/app/sqlite_dump_clean.sql

🔹 What Does This Do?

✅ Verify That Data Was Inserted Successfully

To check if the database has been correctly populated, list all tables:

psql -h <your-rds-endpoint> -U <username> -d <db_name> -c "\\dt"

🔹 What This Does?

✅ Check Specific Tables

To inspect whether data was inserted into key tables, run:

psql -h <your-rds-endpoint> -U grocery_user -d grocerymate_db -c "SELECT * FROM users;"
psql -h <your-rds-endpoint> -U grocery_user -d grocerymate_db -c "SELECT * FROM products;"

🔹 What This Does?

If you see rows of data, the setup is complete! ✅

Next, let run the app on docker

docker run --network host \\
  -e S3_BUCKET_NAME=grocerymate-avatars-20250524115344572600000001 \\
  -e S3_REGION=eu-central-1 \\
  -e USE_S3_STORAGE=true \\
  -e POSTGRES_USER=me**** \\
  -e POSTGRES_PASSWORD=Egwuchu**** \\
  -e POSTGRES_DB=groce**** \\
  -e POSTGRES_HOST=app-db.cv46k0uo8k9f.eu-central-1.rds.amazonaws.com \\
  -e POSTGRES_URI=postgresql://me****:Egwuchu***@app-db.cv46k0uo8k9f.eu-central-1.rds.amazonaws.com:5432/grocerystoredb \\
  -p 5000:5000 grocerymate

Now the app should be running.

You can check in your browser

http://<public ip>:5000

Congratulations🎊