A service can be deployed many times. Store its details once in services. Each run gets its own row in deployments, with a service_id that points back to the service.
A primary key identifies a single row. A foreign key tells PostgreSQL where a referenced value must exist. Compare the IDs in both tables after each insert. Each exercise opens its own database. You can run your CREATE TABLE and INSERT statements together or one at a time.