I tried to locally-host supabase and followed the production recommendations
Tags: devops, opensource, docker, database
Published: 2022-12-24 22:55:54

This is more like my experiences in setting up supabase and some docker configuration stuff... I'm just basically exploring some stuff.

Supabase local-hosting basically uses docker to do 
everything...
while I also use docker for local development, specifically the `devilbox` instance. (which already has pgsql running)

Anyways, Supabase recommend decoupling the pg-database

> It is recommended that you decouple your database from the middleware so that you can upgrade the middleware without any downtime. The "middleware" is everything except Postgres, and it should work with any Postgres provider (such as AWS RDS), or your own Postgres cluster.

So I tried it,

First thing I had to do was add this to my .env file

```
## DB Connection to use Devilbox pgsql
POSTGRES_HOST=pgsql
POSTGRES_DB=supabase
POSTGRES_USER=postgres
POSTGRES_PASSWORD=1234
POSTGRES_PORT=5432
```

now I had to remove the entire `db` section on the `docker-compose.yml` file
and replace it with a network config

```
networks:  
  devilbox_app_net:    
    external: true
```

this network configuration should allow supabase to connect to my devilbox docker bridge network

With this added I had to add a networks config for every container that needs to connect to devilbox-pgsql

```
auth:
  ...
  networks:      
    - devilbox_app_net
  ...
rest:
  ...
  networks:      
    - devilbox_app_net
  ...
realtime:
  ...
  networks:      
    - devilbox_app_net
  ...
storage:
  ...
  networks:      
    - devilbox_app_net
  ...
meta:
  ...
  networks:      
    - devilbox_app_net
  ...
```

Now I had to change all connection-url string that is used in the `docker-compose.yml` file

from `postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres`
to `postgresql://${POSTGRES_USER}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}`

I also had to change `realtime` environments to use `POSTGRES_*` environment values

with all those stuff changed...

I then need to manually install the pgsql extensions needed for supabase to run...

https://supabase.com/docs/guides/self-hosting#extensions

there was no problem installing `uuid-ossp` and `pgcrypto` because they are already in devilbox postgres container.
the problem is `pgjwt` because it's not... I had to manually copy the extension files from host machine to the container and make sure it's in the right directory which is 
`/usr/local/share/postgresql/extension`

extension files can be found in this repository https://github.com/michelp/pgjwt and the important files that I copied are the following:

1. pgjwt--0.1.0--0.1.1.sql
2. pgjwt--0.1.1--0.2.0.sql
3. pgjwt--0.1.1.sql
4. pgjwt.control

After copying those files I was finally able to install the last extension (`pgjwt`)

After making sure that all 3 extensions are properly installed in the devilbox postgres instance,

I had to configure another thing again... 

the devilbox postgres should be configured to have a `wal_level` of `logical` because if not `supabase_realtime` will scream at you. so I fixed that by putting this on my devilbox docker-compose.yml file, under the `pgsql` container configurations.

`command: [ "postgres", "-c", "wal_level=logical" ]`


You might think I'm done!... but I'm not... I'm still stuck!

I'm gettings this error from `supabase_realtime`

`could not access file "wal2json": No such file or directory`

this means that I had to install a wal2json plugin for postgres... installing a plugin on a docker container instance that does not have a `make` command is really hard, and up to this point, I got tired and just gave up...

I know I'm almost there, but the fact the it took this much configuration to just change a database instance literally took me a whole night to research and find solutions.

It's already 6:00 AM! and it's Christmas Morning, I need to sleep!

PS. I started tinkering around 10:00 PM, also this is my first `Dev` post