Homelab/nomad_jobs/services/postgres
2024-02-06 15:30:06 -08:00
..
postgres.nomad.hcl Add Postgres Service 2024-02-06 12:59:15 -08:00
readme.md Fix Typo in Postgres Readme 2024-02-06 15:30:06 -08:00

Postgres

Postgres is a relational database that is open source and widely used. This is a single instance of postgres relying on the host volume for storage. This is not a highly available or fault tolerant setup. The only data redundancy is at the storage layer through ZFS but that is on a single host. If high availability or scalability is a requirement for you, consider a cloud provider like Neon or a more robust setup.

Nomad Job for Postgres

Nomad requires a Host Volume to persist data across restarts. This will limit the portability of the running instance but it is simple to configure. If you want have dynamic storage, you will need to modify the job spec to use a different storage driver such as Ceph or Seaweedfs.

Postgres will have a default user which is a good one to use for making application specific users and databases. These are defined through environment variables in the nomad job spec so you only need to edit the job spec to meet your requirements. If you run it, it will register with consul but be hidden to Traefik meaning you can only access it through the service mesh.

Service Dependencies

TODO

If you want to deploy this, you will need to verify you have a valid host volume and set the initial postgres root credentials.

Line Default Adjustment
17 source = "postgres" Change postgres to a valid host volume name
38 volume = "postgres-data" Change postgres-data to the host volume defined on line 15 if applicable
48 "POSTGRES_USER"="op://InfraSecrets/Postgres Root/username" Change the value to the root username you want. By default, this is a 1password path. See Managing Secrets for more information
49 "POSTGRES_PASSWORD"="op://InfraSecrets/Postgres Root/password" Change the value to the root password you want. By default, this is a 1password path. See Managing Secrets for more information

Make a New Database

You can easily deploy a new postgres database by changing the job name on line 1 and the service name it is exposed as on line 22. You should of course make the other changes mentioned above in the TODO section as well to not cause resource conflicts or use duplicated credentials.

Alternatively, postgres is a relational database management system (RDBMS) meaning we can actually have multiple databases for different applications within a single instance. This is not recommended for production environments as it is a single point of failure. If the postgres instance goes down, all the databases go down but it is a good way to reduce the overhead of running multiple database instances.

You can make a new user and database by entering the exec shell of the postgres container through the nomad UI and run psql to connect using the root credentials. From there run the following example command to make a user and database for your application:

CREATE USER appname WITH PASSWORD 'not-a-secure-password';
CREATE DATABASE appname WITH OWNER appname;

The user and database can be the same name because they are records in different tables but feel free to make them whatever you think is best.