|
| 1 | +# PostgreSQL Setup |
| 2 | + |
| 3 | +I'm making my Postgres setup open source. This is what I use for self-hosting PostgreSQL myself. |
| 4 | + |
| 5 | +It comes with a lot of room for customization and notably: |
| 6 | + |
| 7 | +- Automatic SSL certificate generation/renewals with Traefik as a reverse proxy |
| 8 | +- PgBouncer as a connection pooler that uses auth query instead of userlist.txt |
| 9 | +- Automatic incremental backups to S3-compatible storage |
| 10 | +- Script to create databases and users with granular, scoped permissions within the single cluster |
| 11 | + |
| 12 | +I would like to hear your thoughts, suggestions, and recommendations regarding this setup, and if anything can be improved. |
| 13 | + |
| 14 | +I want to keep this setup small and without bloatware. It can be used as is, but I expect it to be customized according to your needs. |
| 15 | +Current config files expect 4 GB of RAM. If you have less or more, change the settings in the postgres.conf file and the Docker Compose service memory limits. |
| 16 | + |
| 17 | +Currently, tools to view Postgres are not added as part of the setup. |
| 18 | +You can access the database through psql or tools like [pgAdmin 4](https://github.com/pgadmin-org/pgadmin4), [Beekeeper Studio](https://github.com/beekeeper-studio/beekeeper-studio), or [DBeaver](https://github.com/dbeaver/dbeaver). |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +**Assumptions:** You have a Linux server used solely for hosting PostgreSQL with Docker. |
| 23 | + |
| 24 | +### Steps |
| 25 | + |
| 26 | +1. `git clone https://github.com/realchandan/postgres_setup.git` |
| 27 | + |
| 28 | +2. `cd postgres_setup` |
| 29 | + |
| 30 | +3. Copy environment files: |
| 31 | + |
| 32 | + ```bash |
| 33 | + cp .env.example .env |
| 34 | + cp ./config/postgres.env.example ./config/postgres.env |
| 35 | + cp ./config/pgbackup.env.example ./config/pgbackup.env |
| 36 | + ``` |
| 37 | + |
| 38 | + Then, modify the environment files with the appropriate values. |
| 39 | + |
| 40 | + Here's an explanation of environment variables: |
| 41 | + |
| 42 | + > **.env** |
| 43 | + > | Variable Name | Explanation | |
| 44 | + > | --------------- | ---------------------------------------------------------------------------------- | |
| 45 | + > | ACME_EMAIL | The email to be used for ACME/LetsEncrypt | |
| 46 | + > | POSTGRES_DOMAIN | The domain where you want to host the database over SSL, e.g. postgres.example.com | |
| 47 | +
|
| 48 | + > **./config/postgres.env** |
| 49 | + > | Variable Name | Explanation | |
| 50 | + > | ----------------- | ------------------------------------------------------------------------------------------------- | |
| 51 | + > | POSTGRES_DB | The name of the default database. Ideally, you shouldn’t change it (by default, it's postgres). | |
| 52 | + > | POSTGRES_PASSWORD | The password of the PostgreSQL superuser (set a very strong one here). | |
| 53 | + > | POSTGRES_USER | The username of the superuser (ideally, don’t change it). | |
| 54 | +
|
| 55 | + > **./config/pgbackup.env** |
| 56 | + > Refer [here](https://github.com/realchandan/pgbackup?tab=readme-ov-file#usage). If you don't want backups, comment out the pgbackup service in the Docker Compose file. |
| 57 | +
|
| 58 | +4. Point your domain A/AAAA records to the server’s public IPv4/IPv6 addresses. |
| 59 | + |
| 60 | +5. Allow ports 443 and 5432 (TCP) through the firewall. Depending on your firewall, steps may vary. Port 443 is needed for Let’s Encrypt TLS challenge, and 5432 is used by PgBouncer. |
| 61 | + |
| 62 | +6. Add public permissions to the `./config/pg` folder with `chmod -R 777 ./config/pg`. |
| 63 | + |
| 64 | +7. Run `docker compose --env-file .env up -d` to bring up all the services. |
| 65 | + |
| 66 | +8. Create a new database using: |
| 67 | + |
| 68 | + ```bash |
| 69 | + docker exec -it postgres bash -c "/docker-entrypoint-initdb.d/create-user.sh awesome_db passw0rd" |
| 70 | + ``` |
| 71 | + |
| 72 | + This command creates a user called `awesome_db_user` with the password `passw0rd` and gives them access to a database named `awesome_db`. |
| 73 | + |
| 74 | +9. Enjoy and star this repo! (Helps me flex!) |
0 commit comments