Serverless Databases: PostgreSQL
Serverless databases scale up the moment a query arrives and cost nothing while idle, instead of keeping a provisioned instance running around the clock.
This guide shows you how to use PostgreSQL, a powerful, open source object-relational database system, in exactly that fashion. The database runs in a lightweight virtual machine that scales to zero when not in use and resumes in milliseconds on the next connection.
To run it, follow these steps:
-
Install the unikraft CLI. You need a BuildKit builder. The easiest way to get one is via Docker. Alternatively, you can also directly set up and use BuildKit, see the quick start.
-
Clone the
examplesrepository andcdinto theexamples/postgres/directory:Code
Make sure to log into Unikraft Cloud and pick a metro close to you.
This guide uses fra (Frankfurt, 🇩🇪):
Code
When done, invoke the following command to deploy this app on Unikraft Cloud:
Code
The output shows the instance address and other details:
Code
In this case, the instance name is postgres-saan9 and the service young-thunder-fbafrsxj.
They're different for each run.
If you use port 5432/tls per the example above, you can now directly connect to postgres:
Code
Use the unikraft password at the password prompt.
You should see output like:
Code
Use SQL and psql commands for your work.
This example uses the idle scale-to-zero policy by default.
It means that the instance will scale-to-zero even in the presence of psql connections.
The PostgreSQL example makes use of scale-to-zero app support.
This ensures that the instance isn't put into standby even for long running queries (during which the connections are also idle).
To this end, the example loads the pg_ukc_scaletozero module into PostgreSQL, which suspends scale-to-zero during query processing.
You can see this in action by running SELECT pg_sleep(10); and verifying that the instance keeps on running.
You can list information about the instance by running:
Code
Code
When done, you can remove the instance:
Code
Using volumes
You can use volumes for data persistence for your PostgreSQL instance. For that you would first create a volume:
Code
Then start the PostgreSQL instance and mount that volume:
Code
Customize your deployment
Your deployment is a standard PostgreSQL installation. Customizing the deployment means providing a different environment.
An obvious one is to use a different database password when starting PostgreSQL.
For that you use a different POSTGRES_PASSWORD environment variable when starting the PostgreSQL instance.
You could also use a different location to mount your volume or set extra configuration options.
You can use the PostgreSQL instance in conjunction with a frontend service, see the guide here.
Learn more
Use the --help option for detailed information on using Unikraft Cloud:
Code
Or visit the CLI Reference.