{"id":1988,"date":"2021-07-29T12:00:00","date_gmt":"2021-07-29T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/db-migrate-simplified-how-to-generate-posgresql-database-from-node-js\/"},"modified":"2026-07-05T03:26:06","modified_gmt":"2026-07-05T01:26:06","slug":"db-migrate-simplified-how-to-generate-posgresql-database-from-node-js","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/db-migrate-simplified-how-to-generate-posgresql-database-from-node-js\/","title":{"rendered":"DB-Migrate Simplified \u2013 How to Generate PosgreSQL Database From Node.js"},"content":{"rendered":"<p>In this tutorial, you will learn about migrations and how to use it to simplify database operations for your applications. We would be using Node.js with PostgreSQL database. However, the same applied to MySQL and other databases as well.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">What are database migrations<\/a><\/li>\n<li><a href=\"#t2\">Setup Node.js Connection to PostgreSQL<\/a><\/li>\n<li><a href=\"#t3\">Create Migration using db-migrate<\/a><\/li>\n<li><a href=\"#t4\">Generating scripts and Running Migrations<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. What are database migrations<\/strong><\/h5>\n<p>Database migration allows you to make updates to your data model be executing a script in your application. This could be JavaScript or Typescript. The benefit is that your database would always be in sync with the models in your application. Also, when something changes in your database, you will be able to generate the changes. Another benefit is that you can used migrations to initialize a new database when application is deployed.<\/p>\n<p>These script in turn executes SQL statement which run against your database and make the needed changes.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Setup Node.js Connection to PostgreSQL<\/strong><\/h5>\n<p>Now we would create a PostgreSQL database, a Node.js application and connect the two. Follow the steps below to get this done.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a database in PostgreSQL using PgAdmin. I named it migrationdb. Add a table named Users. Add some data<\/p>\n<p><strong>Step 2<\/strong> &#8211; Create a Node.js application.<\/p>\n<p><strong>Step 3<\/strong> &#8211; home.js. The content is as follows:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> {Client} <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'pg'<\/span>)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> client <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Client({\r\n    host<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"localhost\"<\/span>,\r\n    port : <span style=\"color: #333399; font-weight: bold;\">5432<\/span>,\r\n    user<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"postgres\"<\/span>,\r\n    password<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"password\"<\/span>,\r\n    database<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"migrationdb\"<\/span>\r\n})\r\n\r\nclient.connect();\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">let<\/span> query <span style=\"color: #333333;\">=<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Select <span style=\"color: #333333;\">*<\/span> from <span style=\"background-color: #fff0f0;\">\"Users\"<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>;\r\n\r\nclient.query(query, (err, res)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>err){\r\n        console.log(res.rows);\r\n    } <span style=\"color: #008800; font-weight: bold;\">else<\/span>{\r\n        console.log(err.message)\r\n    }\r\n    client.end;\r\n})\r\n<\/pre>\n<p>At his point, you can run the index.js file using the node command, you will see that the data is displayed in the console. Now, we would add migration.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Create Migration using db-migrate<\/strong><\/h5>\n<p>To create migration, we need to install the db-migrate module. To do that,<\/p>\n<p><strong>Step 1<\/strong> &#8211; Run the commands below one after the other:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm install <span style=\"color: #333333;\">-<\/span>g db<span style=\"color: #333333;\">-<\/span>migrate\r\nnpm install -g db-migrate-pg\r\n<\/pre>\n<p>Normally db-migrate also needs to connect to the database.<\/p>\n<p><strong>Step 2<\/strong> &#8211; And to make this connection, you need to create a database.json file with the content<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">{\r\n    <span style=\"color: #007700;\">\"dev\"<\/span>: {\r\n      <span style=\"color: #007700;\">\"driver\"<\/span>: <span style=\"background-color: #fff0f0;\">\"pg\"<\/span>,\r\n      <span style=\"color: #007700;\">\"user\"<\/span>: <span style=\"background-color: #fff0f0;\">\"postgres\"<\/span>,\r\n      <span style=\"color: #007700;\">\"password\"<\/span>: <span style=\"background-color: #fff0f0;\">\"password\"<\/span>,\r\n      <span style=\"color: #007700;\">\"host\"<\/span>: <span style=\"background-color: #fff0f0;\">\"localhost\"<\/span>,\r\n      <span style=\"color: #007700;\">\"database\"<\/span>: <span style=\"background-color: #fff0f0;\">\"migrationdb\"<\/span>\r\n    }\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Now run the command db-migrate &#8211;sql-file<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db-migrate create initialize --sql-file\r\n<\/pre>\n<p>If the command runs successfully, you will notice that a migrations folder is created. This folder contains a sub-folder named sqls which contains two files ending with ..initialize-up.sql and initialize-down.sql. This is where you will write your sql queries.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Creating SQL Scripts and Running Migrations<\/strong><\/h5>\n<p>Now if you know how to write SQL statements you can just write the create table command in the ..initialize-up.sql file. Then write the drop table command in the intialize-down.sql file. But now, let&#8217;s make life easy! Let generate the scripts. Follow the steps.<\/p>\n<p><strong>Step 1<\/strong> &#8211; In PgAdmin, right-click on the Users table and choose Scripts &gt; Create as shown below:<\/p>\n<figure id=\"attachment_14759\" aria-describedby=\"caption-attachment-14759\" style=\"width: 705px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/07\/Screenshot-2021-07-29-at-16.53.08.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-14759\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/07\/Screenshot-2021-07-29-at-16.53.08.png\" alt=\"How to generate PostgreSQL scripts from PgAdmin\" width=\"705\" height=\"459\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/07\/Screenshot-2021-07-29-at-16.53.08.png 705w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/07\/Screenshot-2021-07-29-at-16.53.08-300x195.png 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/07\/Screenshot-2021-07-29-at-16.53.08-600x391.png 600w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/a><figcaption id=\"caption-attachment-14759\" class=\"wp-caption-text\">How to generate PostgreSQL scripts from PgAdmin<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong> &#8211; Copy the generated code into your &#8230;initialize-up.sql file. It should look like this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">-- Table: public.Users<\/span>\r\n\r\n<span style=\"color: #888888;\">-- DROP TABLE public.\"Users\";<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">CREATE<\/span> <span style=\"color: #008800; font-weight: bold;\">TABLE<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span>.<span style=\"color: #aa6600;\">\"Users\"<\/span>\r\n(\r\n    id <span style=\"color: #007020;\">bigint<\/span>,\r\n    firstname <span style=\"color: #007020;\">character<\/span> <span style=\"color: #007020;\">varying<\/span> <span style=\"color: #008800; font-weight: bold;\">COLLATE<\/span> pg_catalog.<span style=\"color: #aa6600;\">\"default\"<\/span>,\r\n    lastname <span style=\"color: #007020;\">character<\/span> <span style=\"color: #007020;\">varying<\/span> <span style=\"color: #008800; font-weight: bold;\">COLLATE<\/span> pg_catalog.<span style=\"color: #aa6600;\">\"default\"<\/span>\r\n)\r\n\r\nTABLESPACE pg_default;\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">ALTER<\/span> <span style=\"color: #008800; font-weight: bold;\">TABLE<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span>.<span style=\"color: #aa6600;\">\"Users\"<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">OWNER<\/span> <span style=\"color: #008800; font-weight: bold;\">to<\/span> postgres;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Write the &#8216;Drop Table&#8217; statement in the &#8230;initialize_down.sql file.<\/p>\n<p><strong>Step 4<\/strong> &#8211; Now, you can delete the table from your database using PgAdmin<\/p>\n<p><strong>Step 5<\/strong> &#8211; In node.js, run the command below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">db-migrate up initialize\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>We are not done yet! There&#8217;s still much to do with migrations. So I have decided to cover all this in the video tutorial. So I recommend you watch the DB-Migration Tutorial on <a href=\"https:\/\/www.youtube.com\/c\/KindsonTheTechPro\" target=\"_blank\" rel=\"noopener\">my YouTube Channel<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about migrations and how to use it to simplify database operations for your applications. We would be using Node.js &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1988","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1988"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions"}],"predecessor-version":[{"id":2156,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions\/2156"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}