How to work with PostgreSQL on Render

Introduction
Render offers PostgreSQL for free accounts, but the database is deleted after one month.
If the data matters to you, the solution is to back it up and recover it.
How to backup
Follow these steps;
Run this command in the terminal to backup the PostgreSQL DB:
pg_dump -h dpg-cn9if6fsc6pc74fh6pd0-a.oregon-postgres.render.com -U mysite mysite_d75u -n public --no-owner > database_dump.sqlWhat each of the values in
1above means can be found in6belowYou will be prompted to input a password. The password can be obtained by following the steps described in
7below.In case your system
pg_dumpdoesn’t match the server version, you might get the error below;pg_dump: error: server version: 16.4 (Debian 16.4-1.pgdg120+2); pg_dump version: 14.13 (Homebrew) pg_dump: error: aborting because of server version mismatchTo resolve this error, use the
pg_dumpthat matches the server’s. Something like so;/opt/homebrew/opt/postgresql@16/bin/pg_dump -h dpg-cn9if6fsc6pc73fd6pc0-a.oregon-postgres.render.com -U mysitte mysite_d75u -n public --no-owner > database_dump.sqlReplace the relevant values with the ones that match yours as described in the render dashboard;
dpg-cn9if6fsc6pc74fh6pd0-a: your Hostnameoregon-postgres.render.com: your database regionmysite: your database_usernamemysite_d75u: your database_name
The values are obtained by the following steps;
Log into render
Go to
projectsUnder
services, click PostgreSQLservice nameThe details will be under the
connectionsaboveAccess Control
How to drop the DB
Follow these steps;
Run this command to drop the PostgreSQL DB:
/opt/homebrew/opt/postgresql@16/bin/dropdb -h dpg-cn9if6fsc6pc73fd6pc0-a.oregon-postgres.render.com -U mysite mysite_d75uYou will be prompted to input the password.
How to create and restore the DB
Follow these steps;
Run this command to create a new PostgreSQL DB:
/opt/homebrew/opt/postgresql@16/bin/createdb dpg-cn9if6fsc6pc73fd6pc0-a.oregon-postgres.render.com -U mysite mysite_d75uRun this to restore your PostgreSQL DB from the backup
psql -h dpg-cn9if6fsc6pc73fd6pc0-a.oregon-postgres.render.com -U mysite mysite_d75u < database_dump.sqlYou will be prompted to input the password
Conclusion
Shout out to Silumesii who greatly assisted with this.
With this guide, you can back up, delete and restore your PostgreSQL DB on render.
I plan to explore a way of automating this process.



