Making PostgreSQL run on the first start of WSL 2 terminal

My daily routine involves working with Ruby on Rails with PostgreSQL as a relational database service. Thus, I will be discussing about how to automate the start of PostgreSQL service on Windows Subsystem Linux (WSL) 2.

The story

I was excited about the release of Windows 11 on the Insider Preview channel. I have enrolled in the program on my Dell XPS laptop and run it for a week. It has been a good experience using it without facing any issues or stupid bugs that are usually packaged together with the unstable release of Windows. Hence, this article is written based on my experience setting it on the latest Windows Preview build 22000.5.

Background

I was following the GoRails tutorial on how to Install Ruby On Rails on Windows 10. The tutorial suggests us install PostgreSQL on Windows and used it on the WSL. I have tried this method, but it doesn’t work out of the box and I believe further tweaks are required which I haven’t figure out the how-to. Since that, I installed PostgreSQL on the WSL which requires me to start the Postgres service each time the first time the WSL is started. 

How to start Postgres service?

Both method works on my WSL to start the postgres service:

  • sudo pg_ctlcluster 12 main start or;
  • sudo service postgresql start

The issue

I used to work with Ubuntu where the postgres service is automatically started upon the operating system startup. Probably, thanks to systemd?

I was shocked every time that I forgot to start the postgres service where I’m executing an Active Record command and it returns an error:

PG::ConnectionBad: could not connect to server: No such file or directory

Since the postgres service is a must for me whenever I start the WSL terminal, I’m thinking about how to automate the start of the services for every first time of opening the terminal. p/s postgres service does not automatically start on WSL.

Writing a simple Shell Script

Every time we start a terminal, the OS will execute .bashrc* file and we are going to modify the file to check if the postgres service is running or not. Why need to check? Since the terminal can be opened multiple times, the services don’t have to start multiple times. Instead only one single time. So, we need to have some mechanism to check the status of the services.

*Note: .bashrc is located at your home directory which usually at /home/<username>/.bashrc

if ! pgrep -x "postgres" >/dev/null; then
    sudo pg_ctlcluster 12 main start
    echo "postgres service started using: sudo pg_ctlcluster 12 main start"
fi

This is what my full .bashrc file looks like.

Another issue

If you immediately start your terminal after setting up your .bashrc file, you will notice that you will be asked for a password since starting service requires you to be a superuser. Hence, we are going to fix this issue by whitelisting the command to not have to input a password.

We are going to create a file in sudoers.d since we don’t want to modify it with the operating system file. This will prevent any issue if the operating system needs to update the actual sudoers file in the event of upgrades. First, we need to create a file where will put the filename as skip_sudo_pg.

$ sudo visudo -f /etc/sudoers.d/skip_sudo_pg

Then, the content of the file

%sudo   ALL=(ALL) NOPASSWD:/usr/bin/pg_ctlcluster

Done! Now, you have set the postgres service to automatically start upon the first time of running the terminal. It’s making a lot of sense to me as I used to work with Rails console with Active Record query every time I accessing the WSL terminal.

WSL 2 Terminal showing a message of postgresql service has been started
Terminal will print postgres service started using: sudo pg_ctlcluster 12 main start when the services are started

If you were on my side, how do you will set your environment other than this way? Share it with me ?.

References

https://www.cyberciti.biz/faq/bash-check-if-process-is-running-or-notonlinuxunix/

https://superuser.com/questions/1343558/how-to-make-wsl-run-services-at-startup

https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers-file


Comments

5 responses to “Making PostgreSQL run on the first start of WSL 2 terminal”

  1. Mina Rizk Avatar
    Mina Rizk

    Thanks a lot for the article

  2. Mastur FYC Avatar
    Mastur FYC

    thanks, very useful

    1. Wan Zulkarnain Avatar
      Wan Zulkarnain

      You are welcome ?

  3. Thanks for the article!

    If you want a slightly cleaner way to check if postgres is already running, use “service postgresql status”, which will return a non-zero exit code if the service isn’t running, so you can do this:

    sudo service postgresql status || sudo service postgresql start

    1. Wan Zulkarnain Avatar
      Wan Zulkarnain

      I totally missed that point. You are welcome! ?

Leave a Reply to Mina Rizk Cancel reply

Your email address will not be published. Required fields are marked *