<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mário Batalha</title>
    <description>The latest articles on DEV Community by Mário Batalha (@mariobatalha).</description>
    <link>https://dev.to/mariobatalha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F516042%2Ff1b045cb-6d04-47cd-80f3-21f610b2220d.png</url>
      <title>DEV Community: Mário Batalha</title>
      <link>https://dev.to/mariobatalha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mariobatalha"/>
    <language>en</language>
    <item>
      <title>How to run migrations in remotely database (Heroku Postgres)</title>
      <dc:creator>Mário Batalha</dc:creator>
      <pubDate>Mon, 23 Nov 2020 18:50:08 +0000</pubDate>
      <link>https://dev.to/mariobatalha/how-to-run-migrations-in-remotely-database-heroku-postgres-4dki</link>
      <guid>https://dev.to/mariobatalha/how-to-run-migrations-in-remotely-database-heroku-postgres-4dki</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;First of all you have to set the target environment database and install the appropriate database library. For this example I'm using Postgres.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm install pg&lt;/em&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npx knex init&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I'm creating two migrations for this example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npx knex migrate:make 01_users&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npx knex migrate:make 01_task&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Save the project and create a repository in GitHub&lt;br&gt;
Link of repository example: &lt;a href="https://github.com/MarioBatalha/exemploDB" rel="noopener noreferrer"&gt;exampleDB&lt;/a&gt;. In heroku dashboard create a app and connect to github repository after that add node buildpack and heroku postgres in add-ons.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open config vars and copy the database URL. Now in your project folder set env variable including database URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Connect to remote database in Heroku&lt;br&gt;
heroku pg:psql heroku_database_name --app app_name&lt;/p&gt;

&lt;p&gt;&lt;em&gt;heroku pg:psql postgresql-shallow-30966 --app exemplodb&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;execute:&lt;br&gt;
heroku run knex migrate:latest --app heroku_database_name&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*heroku run knex migrate:latest --app exempledb&lt;/p&gt;

&lt;p&gt;Link to api: &lt;a href="https://exemplodb.herokuapp.com/user" rel="noopener noreferrer"&gt;exampleDB&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading! I'd love to hear if it works for you, I really hope be help with this article.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>postgres</category>
      <category>heroku</category>
    </item>
  </channel>
</rss>
