DEV Community

Cover image for How to run migrations in remotely database (Heroku Postgres)
Mário Batalha
Mário Batalha

Posted on

How to run migrations in remotely database (Heroku Postgres)

Hi everyone,

In this post I will talk about how to run migrations in remotely database with knexjs in Heroku Postgres. Is required to be installed nodejs and heroku CLI.

  1. First of all you have to set the target environment database and install the appropriate database library. For this example I'm using Postgres.

    npm install pg

  2. The migration CLI is bundled with the knex install, than you have to run knex to execute knexfile.js . After execute knefile.js, open that file configures migrations directory and database connection.

    npx knex init

  3. I'm creating two migrations for this example:

    npx knex migrate:make 01_users

    npx knex migrate:make 01_task

  4. Save the project and create a repository in GitHub
    Link of repository example: exampleDB. In heroku dashboard create a app and connect to github repository after that add node buildpack and heroku postgres in add-ons.

  5. Open config vars and copy the database URL. Now in your project folder set env variable including database URL.

  6. Connect to remote database in Heroku
    heroku pg:psql heroku_database_name --app app_name

    heroku pg:psql postgresql-shallow-30966 --app exemplodb

  7. execute:
    heroku run knex migrate:latest --app heroku_database_name

*heroku run knex migrate:latest --app exempledb

Link to api: exampleDB

Thanks for reading! I'd love to hear if it works for you, I really hope be help with this article.

Top comments (0)