You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
0 Hrs
41 Min
28 Seg

Deploy de Postgres en Heroku

20/22
Resources

How to configure environment variables for database connection in Heroku?

Starting a project in Heroku and connecting to a Postgres database may seem complicated at first, but with the correct configuration of environment variables, the process is simplified. Heroku provides an environment variable called DATABASE_URL that encapsulates all the data needed for the connection, eliminating the need to specify parameters such as user, password, host, and port individually.

What changes should be made in the project to read the DATABASE_URL variable?

In our project it is essential to make changes to be able to read the DATABASE_URL variable. Before, we had the connection very segmented:

  • Protocol (e.g. Postgres)
  • User
  • Password
  • Host
  • Port

Now, we must integrate these elements into a single URL in a standard format, both for development and production environments. In this context, the URL format would be something like:

postgres://user:password@host:port/database

How to integrate the configuration into the database modules?

Once the connection URL is obtained, we need to modify the project's database modules. Here, we will go from reading individual variables to only needing the connection URL. The steps would be as follows:

  • In the module where we read the database configuration, we would change the current method to reading the POSTGRES_URL variable.
  • The POSTGRES_URL variable would correspond to the value of DATABASE_URL provided by Heroku.

For example:

const databaseURL = process.env.POSTGRES_URL;// Use `databaseURL` for the connection to the DBMS.

What settings should be made before deployment in Heroku?

Before proceeding with the deployment, some settings are necessary:

  1. Additional environment variables: some settings, like DATABASE_NAME, PORT, or secrets like API_KEY and JW_SECRET, must be managed locally and in Heroku. Make sure these keys are not lost in production.

  2. Validations: In the variable validation section, make sure to verify that DATABASE_URL is present when running on Heroku.

  3. SSL configuration: Enable the required SSL configuration. To do this, add the following in your connection settings:

    ssl: { rejectUnauthorized: false }

This setting is critical for securing data transmissions in a production environment.

How to verify and debug after deployment?

Once the deployment is done, it is crucial to verify that the connections are successful. This can be done by visiting the URL where the deployment was performed. Similarly, monitoring logs can provide valuable information for troubleshooting.

To track logs in real time, use the following command:

heroku logs --tail

In case of database-related errors, make sure you have run all the migrations that your database schema requires. This step is essential, and its lack can result in error messages indicating non-existent tables or records.


The correct configuration of environment variables and the adaptation of the project to use DATABASE_URL significantly simplify the deployment process in Heroku. Keep the configuration tidy and systematically check the proper functioning with logs to combat possible errors. Keep going and don't stop exploring new ways to optimize and improve your Heroku workflow!

Contributions 2

Questions 3

Sort by:

Want to see more contributions, questions and answers from the community?

馃憦

DATABASE_URL=postgres://root:123456@localhost:5432/my_db