Deploying a PostgreSQL Database on Render

Pasindu Chamod Madusha
3 min readJan 30, 2024

--

I will walk you through the process of deploying your locally running PostgreSQL database to a Render server. For this demonstration, I’m utilizing a project initiated with Node.js, Express, and TypeScript.

To begin, ensure that you have a Render account. Once you’ve created an account, click on the ‘New+’ button at the top right corner. A dropdown menu will appear with various options. Locate the PostgreSQL option, which is represented by a database icon, as shown in the figure below. Click on it, and you will be directed to a new dedicated page.

New PostgreSQL Database Server

Once you’re on this page, you’ll encounter several text fields that require input. The ‘Name’ field requests a name for your server, and the ‘Database’ field asks for a name for your database (you can use the name of your local machine’s database here). The ‘User’ field requires a username; if left empty, a random username will be assigned.

For the ‘Region,’ select the most appropriate region based on your location. Set the PostgreSQL version to 15, the latest version available. The ‘Datadog API key’ is optional, you can choose to integrate Datadog, an observability platform for cloud-scale applications, for enhanced metrics, monitoring, and automated alerting. Below is an example with the fields filled out. After filling these fields and choose a suitable plan for you and then hit Create Database button.

Database Initialization

Subsequently, you will be directed to the Render dashboard, and the process of creating your database will commence. Please be patient as this may take some time. Once completed, you’ll be able to view your database status along with pertinent information, as illustrated below:

Database Connections

Now, you’ll require these values to configure the server and make adjustments to the server code file on your local device. If you have a code section resembling the one below where the database is initialized, modify it accordingly.

export const AppDataSource = new DataSource({
type: "postgres",
host: process.env.LOCALHOST,
port: 5432,
username: process.env.DB_USERNAME,
password: process.env.PASSWORD,
database: process.env.DATABASE,
synchronize: true,
logging: false,
entities: [Student],
});

Now, modify the function as follows. Utilize the external link found in the Render dashboard where you created your database. Insert that link into the URL property as illustrated below.

export const AppDataSource = new DataSource({
url: process.env.DEPLOYED_URL,
logging: false,
entities: [Student],
ssl: {
rejectUnauthorized: false,
},
migrations: [],
subscribers: [],
});

You have successfully configured your local database on a server, and it should function properly. Run your index.ts file (server file) to confirm that the database is connecting. For further verification, you can use a tool like Postman to check if the database behaves as expected.

If you encounter an error like Error while connecting to the database error: SSL/TLS required, you can resolve it by adding the following code snippet:

ssl: {
rejectUnauthorized: false,
}

Alternatively, for a production build, it is recommended to set up an SSL certificate. The reason for this error is that the PostgreSQL server requires a secure connection using SSL/TLS, and your application is not configured to use it.

ps: In case if you want to connect your deployed database to your local devices’s pgAdmin and create or edit data you can check this article for that. https://medium.com/@epcm18/connect-your-deployed-postgresql-database-to-pgadmin-8880e838de0c

--

--