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.
-
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
-
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
-
I'm creating two migrations for this example:
npx knex migrate:make 01_users
npx knex migrate:make 01_task
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.Open config vars and copy the database URL. Now in your project folder set env variable including database URL.
-
Connect to remote database in Heroku
heroku pg:psql heroku_database_name --app app_nameheroku pg:psql postgresql-shallow-30966 --app exemplodb
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)