Convert Any Cloud Linux Instance To PostgreSQL Server.

Sumit Kukade
2 min readDec 15, 2019

--

Recently I had this requirement to use psql cloud instance for some work and while doing it I found that what if I create just a cloud Linux instance and configure it as psql server then will it be cheaper than using ready-made psql server instance offered by cloud service providers?

Guess what?? Yes It is way cheaper like approximately 50%. So I would like to share in this article what I did to pull this stunt!

How to convert any cloud Linux instance to psql server:

I wrote a Python script to configure a clound Linux instance as psql server. You can directly read and use my script. Script link is here.
Though I am going to explain the process so that you can also write your own!

Steps I followed:

  1. First I created a cloud Linux instance and connected to it via SSH.
  2. Then I have installed PostgreSQL 11 (which is latest while I am writing this).
  3. Then I created a PostgreSQL URL using random credentials to make URL unique.
## Creating postgres url here
DATABASE = “postgres”
PORT = “5432”
HOST = socket.gethostbyname(socket.gethostname())
USER = “”.join( [random.choice(string.ascii_lowercase) for i in xrange(14)] )
PASSWORD = “”.join( [random.choice(string.ascii_lowercase + string.digits) for i in xrange(64)] )
NAME = “”.join( [random.choice(string.ascii_lowercase) for i in xrange(14)] )
db_url = DATABASE+”://”+USER+”:”+PASSWORD+”@”+HOST+”:”+PORT+”/”+NAME

4. Then I have created a psql user and gave it role and permission as needed.

5. Then added configuration like ‘Allow IP’, ‘listen_addresses’ etc.. to file at “/etc/postgresql/11/main/pg_hba.conf”

6. As last step of the script will return ‘db_url’ created earlier and also it will save to file.

Bingo!! Now we can connect to our psql server using psql URL generated by our script.

Although we can create our own psql server using cloud Linux instance but there are some drawbacks of doing it. We do not get facilities like auto-scaling, regular backup etc.

But if do not wish or need to use all those facilities (or we can write scripts to do that) then we do have to pay extra money for this service.
We can just take any regular cloud Linux instance and configure it as psql server. That is all.

I hope that this article has helped you in some way, I will write follow up articles about this too. Criticism is always welcome!

Script: https://gist.github.com/sumitkukade/711a3e5a2a13b9b455b1d24b470a1adb

--

--