Running Startup Scripts to Run Metabase after Migrating to Postgres

I am running a locally hosted instance of Metabase on my Windows 10 laptop. I had setup a startup service to automatically start Metabase on my computers startup using the nssm utility as outlined here

After migrating to Postgres as my application database for Metabase, I am trying to set my startup service so that when the service starts, it uses the Postgres DB as the application database as opposed to H2 however it seems like it wants to recreate the H2 database every time and starts my whole instance of Metabase from scratch.

Is there any way to configure the startup service using the nssm utility so that it recognizes the Postgres DB?

EDIT: I figured out how to do this. See my solution below.

Hi! there should be any env var missing or some problem about that software not being able to read from the env vars

Hi @Luiggi thanks for the input. I'm not sure I understand what you are explaining?

I'm guessing that the issue here is that when Metabase launches, it does not see (for some reason) the environment variables that have the connection details and that's why you are seeing that it creates the H2 database over and over. Can you check if the environment variables can be read?

Thanks @Luiggi that makes sense. This might be a little beyond my expertise. How would I check to see if the environment variables are being read?

I really don't know anything about nssm, but I'm sure you run a BAT file? if that's the case, try setting the env vars on that BAT file so you make sure that the Metabase service is launched with all the necessary configurations to connect to the DB

Thanks again @Luiggi The nssm utility was recommended in another post (see my link above). It's a simple third party utility that provides a GUI to create a windows service.

That said, although you didn't solve my problem, you pointed me in the right direction by making me think through the whole idea about setting the environment variables, so many thanks!

For anyone else who is looking to do something similar, this is what I did:

  • Follow the instructions to install a service using the nssm install utility as outlined in the link I posted above. Fill the information out on the 'Application' tab but do not install the service yet

  • Scroll to the right, you will find an 'Environment' tab:

image

  • Enter the following environment variables:

    MB_DB_TYPE=postgres
    MB_DB_DBNAME=your_postgres_db_name
    MB_DB_PORT=your_postgres_port_number
    MB_DB_USER=your_postgres_username
    MB_DB_PASS=your_postgres_password
    MB_DB_HOST=localhost

    Note: I did not enable the option to 'Replace default environment...'

  • Now install the service

  • Restart your computer and the service should start running on startup. Navigate to your localhost and see if Metabase is running and log in. (if service is not running, go to the properties of the service and make sure startup type is set to 'Automatic')

Of course, this is all dependent on the fact that you have installed Postgres on your computer and that you have created an empty database. Also, that you have migrated Metabase to that database following Metabases instructions here

I would recommend one addition thing. The only way I knew that this worked is that I backed up my H2 database files and then deleted them from the Metabase install location. After I re-started, I monitored the install location to see if Metabase re-built the H2 db files and it did not. If it does, that means your instance of Metabase is not utilizing the Postgres database and you will need to re-configure.

Cheers,
Mike

1 Like

many thanks for posting this and feeding the community with all this knowledge!

2 Likes