<?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: Trenten Grede</title>
    <description>The latest articles on DEV Community by Trenten Grede (@tgrede).</description>
    <link>https://dev.to/tgrede</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%2F638556%2F096b859f-7fae-4e04-8545-64b52ef5e3f5.jpeg</url>
      <title>DEV Community: Trenten Grede</title>
      <link>https://dev.to/tgrede</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tgrede"/>
    <language>en</language>
    <item>
      <title>How to set up a Postgres Database with Heroku</title>
      <dc:creator>Trenten Grede</dc:creator>
      <pubDate>Fri, 02 Jul 2021 15:45:49 +0000</pubDate>
      <link>https://dev.to/tgrede/how-to-set-up-a-postgres-database-with-heroku-31i5</link>
      <guid>https://dev.to/tgrede/how-to-set-up-a-postgres-database-with-heroku-31i5</guid>
      <description>&lt;h1&gt;
  
  
  Motivation
&lt;/h1&gt;

&lt;p&gt;I realized I can only stumble my way through setting up a database and deploying to Heroku. I want to be certain of my steps and set it up from scratch while documenting the steps I take. This is for my own future reference, and to solidify my skills, as well as to guide others who want a step by step introduction to setting up a production database on Heroku.&lt;/p&gt;

&lt;h1&gt;
  
  
  Requirements and Prerequisites
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git" rel="noopener noreferrer"&gt;Git&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Git is used for version control. It will provide us &lt;br&gt;
with a convenient command line interface to save &lt;br&gt;
different versions of our code and commit them to github&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Github is a code hosting platform that hosts a Remote &lt;br&gt;
Repository of our code. We can use Git to push our code &lt;br&gt;
to Github and share it with the world.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;Postgres&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Postgres is the database system we will be using for &lt;br&gt;
this project. There are others like SQLite, MySQL, &lt;br&gt;
Oracle, and more. But I know Postgres so that is what I &lt;br&gt;
will be showing in this guide.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pgadmin.org/download/" rel="noopener noreferrer"&gt;pgAdmin4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;pgAdmin is technically not necessary for any part of &lt;br&gt;
this process. I use it in my workflow because I like the &lt;br&gt;
GUI it provides to manage the database and write SQL &lt;br&gt;
queries.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://devcenter.heroku.com/articles/heroku-cli" rel="noopener noreferrer"&gt;Heroku CLI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Heroku CLI is the command line interface for Heroku. &lt;br&gt;
This is going to allow us to push our git code to Heroku, &lt;br&gt;
and there are other useful functions for dealing with &lt;br&gt;
Heroku in there.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;You can use your text editor of choice, mine is VSCode&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;This is javascript runtime to run javascript code &lt;br&gt;
outside of the browser. This will also allow us to run &lt;br&gt;
&lt;code&gt;npm&lt;/code&gt; commands to install dependencies into our project. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Those are the main tools we will need and the appropriate installation links for each. &lt;/p&gt;

&lt;p&gt;Side note: For those still a little unsure, here is a very quick video explaining the difference between Git and GitHub.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/caHOmK9kFfI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And for those who need a longer introduction to git and GitHub here's this.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/DVRQoVRzMIY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Once we have all of this installed and configured we can get started on the project!&lt;/p&gt;

&lt;h1&gt;
  
  
  First Steps
&lt;/h1&gt;

&lt;p&gt;First we have to create a new project. This depends on your system. For me, I use Linux and I have a side-projects folder on my desktop. In it I created a new folder called &lt;code&gt;production-db-tutorial&lt;/code&gt;. To do this I open a new terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq67l0odqn7glbdsb8qgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq67l0odqn7glbdsb8qgx.png" alt="New Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and run &lt;code&gt;cd ~/Desktop/side-projects&lt;/code&gt; to change directories.&lt;/p&gt;

&lt;p&gt;once here I run &lt;code&gt;mkdir production-db-tutorial&lt;/code&gt; to create the new project directory.&lt;/p&gt;

&lt;p&gt;next I can &lt;code&gt;cd production-db-tutorial&lt;/code&gt; to move to my newly created project folder and run &lt;code&gt;code .&lt;/code&gt; to open it in Visual Studio Code.&lt;/p&gt;

&lt;p&gt;All of those commands looked a little something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxf470lvszczb5gfwtfrv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxf470lvszczb5gfwtfrv.png" alt="Terminal After Commands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That &lt;code&gt;code .&lt;/code&gt; command opened a new VSCode window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffypml7zam2iv7wxwcl5n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffypml7zam2iv7wxwcl5n.png" alt="Blank VSCode Window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have our VS Code window open we can open a new terminal to the project directory. Right off the bat I will add a file called &lt;code&gt;.env&lt;/code&gt; and a file called &lt;code&gt;index.js&lt;/code&gt; to the root of the project. Then we can run &lt;code&gt;npm init -y&lt;/code&gt; to create a new &lt;code&gt;package.json&lt;/code&gt; file as well. This is where we will track the metadata of our application and can add &lt;code&gt;npm&lt;/code&gt; scripts to shorten our command line syntax later. &lt;/p&gt;

&lt;p&gt;I will start by installing all the dependencies needed for this tutorial. &lt;/p&gt;

&lt;p&gt;First add start and server scripts to the &lt;code&gt;package.json&lt;/code&gt; so we will be able to run the application. My scripts object looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "node index.js",
    "server": "nodemon index.js",
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run &lt;code&gt;npm i -D nodemon&lt;/code&gt; and this will install nodemon as a dev dependency. Finally run &lt;code&gt;npm i cors dotenv express knex pg&lt;/code&gt; to install cors, dotenv, express, knex, and pg as your normal dependencies. After that your &lt;code&gt;package.json&lt;/code&gt; should look a little like this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5r7orz7ro95awr8yirza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5r7orz7ro95awr8yirza.png" alt="vscode after scripts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take note of the newly added dependencies section and devDependencies section in your &lt;code&gt;package.json&lt;/code&gt; file. Also take note of the &lt;code&gt;package-lock.json&lt;/code&gt; and the node_modules folder. If you have followed up to this point you are currently on track to setting up a database!&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up the Server
&lt;/h1&gt;

&lt;p&gt;I like to start any node project by creating a basic server to make sure everything is running smoothly up to this point. &lt;/p&gt;

&lt;p&gt;Within the &lt;code&gt;.env&lt;/code&gt; file lets create some environment variables for the project (these will be explained when we implement them later)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PORT=5000
NODE_ENV=development
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your file it will look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy56yge2d2to6a2sgdnve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy56yge2d2to6a2sgdnve.png" alt="Env File 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once that's done open up the &lt;code&gt;index.js&lt;/code&gt; file and create the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config()

const express = require('express')
const cors = require('cors')

const server = express()
const port = process.env.PORT || 8000

server.use(cors())
server.use(express.json())

server.get('/', (req, res) =&amp;gt; {
    res.send('&amp;lt;h1&amp;gt;This is a test application&amp;lt;/h1&amp;gt;')
})

server.listen(port, () =&amp;gt; {
    console.log(`\n=== Server listening on port ${port} ===\n`)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Woah woah woah&lt;/strong&gt; lets slow down there and go through this line by line real quick just in case this looks foreign to anyone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line allows us to access the 'environment variables' that we defined in our &lt;code&gt;.env&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const cors = require('cors')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how we import &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;express&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/cors" rel="noopener noreferrer"&gt;cors&lt;/a&gt; into our project.&lt;/p&gt;

&lt;p&gt;Express.js is a web framework that will assist us in creating our HTTP server. Cors stands for Cross-Origin-Resource-Sharing and, at a basic level, allows servers from different origins to request information from each other.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = express()
const port = process.env.PORT || 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are using express to create a server, and then defining our port variable. port is going to look at &lt;code&gt;process.env.PORT&lt;/code&gt; first, so in this case it will access &lt;code&gt;PORT=5000&lt;/code&gt; from the &lt;code&gt;.env&lt;/code&gt; file we created earlier and use 5000 as the port. But if the server doesn't find a PORT in the &lt;code&gt;.env&lt;/code&gt; then it will default to use port 8000 as a backup by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.use(cors())
server.use(express.json())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are telling our server to use cors (remember that talk about cross origin resource sharing earlier? This is enabling that within our server) and we are also telling our server to use express.json() which is a body-parser. This will parse incoming requests with JSON payloads. &lt;a href="https://www.geeksforgeeks.org/express-js-express-json-function/" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is a quick explanation that goes more in depth than I want to in this guide if you really want to know more about it.&lt;/p&gt;

&lt;p&gt;Next we have our basic route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.get('/', (req, res) =&amp;gt; {
    res.send('&amp;lt;h1&amp;gt;This is a test application&amp;lt;/h1&amp;gt;')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This route is how we will access the application in the browser as I will demonstrate very shortly.&lt;/p&gt;

&lt;p&gt;Lastly we have our listener.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.listen(port, () =&amp;gt; {
    console.log(`\n=== Server listening on port ${port} ===\n`)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This chunk of code is using our port variable, and telling our server to listen on that port, while its listening on that port the console will display that text to let us know its working.&lt;/p&gt;

&lt;p&gt;When all that is put together it should look like this in your Visual Studio Code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful8cjir6qqwwziqu4kqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful8cjir6qqwwziqu4kqg.png" alt="Index File Server Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that is all squared away and everyone is up to speed we can actually &lt;em&gt;run&lt;/em&gt; the server and access the route from the internet browser to make sure our code is actually working and doing something. Make sure everything is saved, then open a new terminal to the project directory. Run &lt;code&gt;npm run server&lt;/code&gt; to start the server with nodemon. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwx7aizxcsnq9sovffvr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwx7aizxcsnq9sovffvr1.png" alt="Terminal Server Running"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see this then you've successfully started your server. Now if you open your internet browser to &lt;code&gt;http://localhost:5000/&lt;/code&gt; you should see this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkjqmpzqfiiiaw5sjtoi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkjqmpzqfiiiaw5sjtoi.png" alt="localhost opened"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Uploading to GitHub
&lt;/h1&gt;

&lt;p&gt;At this point you have confirmation that your code is working and you've configured things correctly so far. Now is a perfect time to make a first Git commit and upload to GitHub. Create a new GitHub Repo and it should look a little something like this before you've uploaded your code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmuisdodk9swwtnhpdcr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmuisdodk9swwtnhpdcr.png" alt="Github new repo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now back in the local terminal we are going to turn our project into a local Git repository. &lt;/p&gt;

&lt;p&gt;Run the command &lt;code&gt;npx gitignore node&lt;/code&gt;. This will create a &lt;code&gt;.gitignore&lt;/code&gt; file that tells git what we don't want it to save in version control. This is very useful to store secrets like API keys. Then run &lt;code&gt;git add .&lt;/code&gt; to add all of our current files, and &lt;code&gt;git commit -m 'first commit'&lt;/code&gt; to commit our changes with a commit message. &lt;code&gt;git branch -M main&lt;/code&gt; changes our primary branch from master to main. Follow this by adding our remote repository locally. I did this by running &lt;code&gt;git remote add origin https://github.com/tgrede/production-db-tutorial.git&lt;/code&gt;. Once added you can push by running &lt;code&gt;git push -u origin main&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;I essentially followed these steps provided by github, except instead of &lt;code&gt;git add README.md&lt;/code&gt; I used &lt;code&gt;git add .&lt;/code&gt; to add all the changed files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fybvo5gxe015wy3q13gcb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fybvo5gxe015wy3q13gcb.png" alt="Github Commands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once all those commands have been run you should see that initial repository page turn into this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0mg2ditybfpbd8ectyer.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0mg2ditybfpbd8ectyer.png" alt="GitHub After Pushing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following the push we can start setting up our database. As I said earlier, I use pgAdmin 4 as a GUI and Postgres as the database. If you are unfamiliar with these at all or just want a different take on it, here is a good video tutorial on setting both up.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/C93Ed8b8Mhc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up the Dev Database
&lt;/h1&gt;

&lt;p&gt;Once you have Postgres installed and pgAdmin 4 installed we can move on. When you log in to PG Admin for the first time you'll see a screen like this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjfeo81hqcqoma3dugnm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjfeo81hqcqoma3dugnm.png" alt="pgAdmin Blank"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right click on servers, click create, click server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj69vyl9ftwzh8yw1nij6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj69vyl9ftwzh8yw1nij6.png" alt="pgAdmin Create Server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see a pop up in the middle of your screen with configuration options for your new database server.&lt;/p&gt;

&lt;p&gt;I entered a name like 'learning' and then under the second tab set the host to 'localhost' and the port to 5432. The username for mine is postgres and I needed to enter the password I created for postgres in the password box. This is all I needed to enter to create the server&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxh9rb2rycgiulhc6p8fd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxh9rb2rycgiulhc6p8fd.png" alt="newServer1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc9hvpb9xx9a0sd9viols.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc9hvpb9xx9a0sd9viols.png" alt="newServer2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This might take some playing around with to get it working as it depends how you set up postgres on install. But once it accepts your server configuration you are all set to start connecting to it.&lt;/p&gt;

&lt;p&gt;Next open servers, open your new server, and you'll see 'Databases'. Right click Databases, then click create. This will bring up the dialogue to create a new database on the database server&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F61jcvt56zqp4ivxse38c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F61jcvt56zqp4ivxse38c.png" alt="right click new database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This should open a pop up where you can enter the name of the new database. I'll just call mine tutorial-db.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7t3z05hjtcia2qfqwpdk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7t3z05hjtcia2qfqwpdk.png" alt="pop up create new db"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After saving you'll see in the side bar on the left your new database listed in the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ow7efppr6ayp7lcbl5k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ow7efppr6ayp7lcbl5k.png" alt="after creating new db"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all we need to do within pgAdmin for the moment. Back in our code its time to write the configuration files to actually migrate and seed our database.&lt;/p&gt;

&lt;h1&gt;
  
  
  Database Environment Variables
&lt;/h1&gt;

&lt;p&gt;Update your environment variables in your &lt;code&gt;.env&lt;/code&gt; file to match the information from your database configuration that you just went through. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3q6v7oyk28lbzxer4dwr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3q6v7oyk28lbzxer4dwr.jpg" alt="env variables for database"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;DB_HOST&lt;/code&gt; should be the host/address from when you created your server, in my case I used localhost as my host. &lt;br&gt;
&lt;code&gt;DB_USER&lt;/code&gt; by default should be postgres. postgres is a superuser that is created when you install postgres and so by default postgres is the owner of the postgres database server.&lt;br&gt;
&lt;code&gt;DB_PASS&lt;/code&gt; I blurred for obvious reasons but should be the password you set in pgAdmin4 when setting everything up.&lt;br&gt;
&lt;code&gt;DB_NAME&lt;/code&gt; is the name of the database you created, in my case it was &lt;code&gt;tutorial-db&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and those should be all the environment variables to set on your local machine. &lt;/p&gt;
&lt;h1&gt;
  
  
  Starting with knex
&lt;/h1&gt;

&lt;p&gt;We need to start by running the command &lt;code&gt;knex init&lt;/code&gt;. This command uses the knex library we installed earlier to create a default &lt;code&gt;knexfile.js&lt;/code&gt; in the root of our project.&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;knexfile.js&lt;/code&gt; and replace the code with this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config()
const pg = require('pg');


if (process.env.DATABASE_URL) {
  pg.defaults.ssl = { rejectUnauthorized: false }
}

const sharedConfig = {
  client: 'pg',
  migrations: {directory: './data/migrations'},
  seeds: {directory: './data/seeds'},
}

module.exports = {
  development: {
    ...sharedConfig,
    connection: {
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASS,
      database: process.env.DB_NAME
    }
  },
  production: {
    ...sharedConfig,
    connection: process.env.DATABASE_URL,
    pool: { min: 2, max: 10 },
  },
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once again I will not leave you in the dark and will explain our code and how it all connects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line is there to allow the code to access our .env file to get our environment variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pg = require('pg');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty self explanatory but this imports our pg library into the code for our next code block to make use of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (process.env.DATABASE_URL) {
  pg.defaults.ssl = { rejectUnauthorized: false }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is standard and required by the production database. Notice we never set a &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable. that's because this code snippet wont do anything in development but is necessary when we run this all on Heroku later where there &lt;strong&gt;is&lt;/strong&gt; a &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable.&lt;/p&gt;

&lt;p&gt;After that we have a variable we define to simplify things.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sharedConfig = {
  client: 'pg',
  migrations: {directory: './data/migrations'},
  seeds: {directory: './data/seeds'},
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets the client as pg. This also sets our migrations directory and seeds directory so that knex knows where to look in our repository when we run migrate and seed commands.&lt;/p&gt;

&lt;p&gt;The reason we set that as a &lt;code&gt;sharedConfig&lt;/code&gt; variable is because those config variables are shared between our development and production environments. It saves us some typing down here in the last piece of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  development: {
    ...sharedConfig,
    connection: {
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASS,
      database: process.env.DB_NAME
    }
  },
  production: {
    ...sharedConfig,
    connection: process.env.DATABASE_URL,
    pool: { min: 2, max: 10 },
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this piece of code sets the connection configuration settings for the development and production environment. &lt;code&gt;...sharedConfig&lt;/code&gt; is our variable from before setting the client, migration, and seed configurations. then we have a &lt;code&gt;connection&lt;/code&gt; variable. &lt;code&gt;connection&lt;/code&gt; tells knex where the database lives and creates a connection with the database server allowing us to populate it with data and interact with it using our code. The last unexplained bit is this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pool: { min: 2, max: 10 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This photo explains it pretty well honestly. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpa8zs6humz28dccobe4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpa8zs6humz28dccobe4e.png" alt="connection pool explanation"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So now that explains our &lt;code&gt;knexfile.js&lt;/code&gt; and it should look like this now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdemqu2l9wjbd9qx500ht.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdemqu2l9wjbd9qx500ht.png" alt="knex file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Migrating and Seeding
&lt;/h1&gt;

&lt;p&gt;Now we can work on migrating and seeding our development database. Run the commands to create new migration and seed files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;knex migrate:make first-migration
knex seed:make 001_first-seed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is my terminal view as an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0ke6cdo4x80i3sci5ol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0ke6cdo4x80i3sci5ol.png" alt="Create Migration And Seed Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember in the &lt;code&gt;knexfile.js&lt;/code&gt; where we did this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sharedConfig = {
  client: 'pg',
  migrations: {directory: './data/migrations'},
  seeds: {directory: './data/seeds'},
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was telling knex where to place our newly created migration and seed files as shown here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faru7uqpeasd7g7gepmip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faru7uqpeasd7g7gepmip.png" alt="Sidebar Migrations and Seeds"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can add code to these new files. Open your new file in your migrations folder and change the code to this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.up = (knex) =&amp;gt; {
    return knex.schema
        .createTable('profiles', function (table) {
            table.increments('profile_id');
            table.string('name', 128).notNullable();
        });
    };

exports.down = (knex) =&amp;gt; {
    return knex.schema.dropTableIfExists('profiles');
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a Knex Tutorial but if you want to read some more knex documentation you can find it &lt;a href="https://knexjs.org/#Schema" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code is essentially a basic migration that adds a new table to our database, giving an auto incrementing profile_id column as well as a name column. Your migration file will look something like this if you're following along.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmxgbpxye08y5jay786mp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmxgbpxye08y5jay786mp.png" alt="First Migration File"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we can add our seed code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const profiles = [
  {
    name: 'John'
  },
  {
    name: 'Abigail'
  },
  {
    name: 'Patty'
  },
  {
    name: 'Sally'
  },
  {
    name: 'Gary'
  }
];

exports.seed = function (knex) {
  return knex('profiles').del()
  .then(() =&amp;gt; {
    return knex('profiles').insert(profiles)
  })
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will just add a bunch of names to our development database to test it out. It should leave your seed file looking like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvw7y4bh30kv6g0v5vs82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvw7y4bh30kv6g0v5vs82.png" alt="First Seed File"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have our migration file written and seed file written we can actually RUN the migration and seed commands to populate the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;knex migrate:latest
knex seed:run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands will take your migration file and create the table. Then it will take your seed file and pre-populate the table with data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnz7fq1sqo1bkz5x3xom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnz7fq1sqo1bkz5x3xom.png" alt="terminal after running migration and seed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next open pgAdmin and look at the left side bar. Right click &lt;code&gt;Databases&lt;/code&gt; and click refresh. You should now be able to expand your database, then schemas, then tables, to see your newly created database tables. It should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxaewhwjaywofutyllsg4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxaewhwjaywofutyllsg4.png" alt="pgAdmin Left SideBar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can now right click on &lt;code&gt;profiles&lt;/code&gt; and click &lt;code&gt;view/edit data&lt;/code&gt; and then click &lt;code&gt;all rows&lt;/code&gt; to view your seed data in your table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7ctm1dgx2ylp7libj6y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7ctm1dgx2ylp7libj6y.png" alt="pgAdmin Data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have made it this far you have successfully set up a development database. At this point you could use this knowledge and keep practicing your Knex skills outside of a production environment. However, we now want to deploy this database to Heroku. How do we do that? &lt;/p&gt;

&lt;h1&gt;
  
  
  Deploying to Heroku
&lt;/h1&gt;

&lt;p&gt;First lets re commit our code to GitHub so everything is up to date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m 'second commit'
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then login to Heroku where we will be deploying our application.&lt;/p&gt;

&lt;p&gt;When logged into Heroku I see a list of apps and a &lt;code&gt;new&lt;/code&gt; button in the top right corner. I create a new app using the &lt;code&gt;new&lt;/code&gt; button in the top right and click &lt;code&gt;create new app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firajvj9xt579goadw7e2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firajvj9xt579goadw7e2.png" alt="heroku main screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then name your Heroku app. I will call mine the same thing I have called it this whole time, 'production-db-tutorial'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh67cqim4kiq6hjeq77fj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh67cqim4kiq6hjeq77fj.png" alt="heroku create app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once created you'll be greeted with a screen that looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firjxgtu8ca36lxet4uqb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firjxgtu8ca36lxet4uqb.png" alt="heroku after create screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I would follow the first instruction that says to Login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4ha3tf82b2ml2bmqmt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4ha3tf82b2ml2bmqmt7.png" alt="Heroku Login Command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I would follow the instruction that says how to add a remote heroku repository to your local git repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg76yrnssenquo60oty63.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg76yrnssenquo60oty63.png" alt="Heroku Remote Add"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly deploy your application with this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And just like that our back end code is pushed to Heroku. &lt;br&gt;
&lt;a href="https://production-db-tutorial.herokuapp.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt; is the site I just deployed! Hopefully you have been able to deploy your own application up to this point. Now we can create the database! &lt;/p&gt;
&lt;h1&gt;
  
  
  Adding the Heroku Postgres Database
&lt;/h1&gt;

&lt;p&gt;Scroll back up in Heroku and find the resources tab in your project then use the search bar to find &lt;code&gt;heroku postgres&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5py0am6l8tz5wz99gbx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5py0am6l8tz5wz99gbx.png" alt="Heroku Resources"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you add it to your project an order form will pop up, I'll be using a free database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5ek8asohs91g4qq4v9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5ek8asohs91g4qq4v9o.png" alt="DB order form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After submitting my order it shows up as an add-on in my resources.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5hhgwyfqgt60s4uavry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5hhgwyfqgt60s4uavry.png" alt="DB Created"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I can now Click on this database link and see it created a database within Heroku. How do we access this database now? Well you pushed your code to your Heroku repository, so Heroku has your migration and seed files ready to go. If you now go to your settings tab instead of resources you'll see this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4lfj4segd2id3sdf214.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4lfj4segd2id3sdf214.png" alt="Heroku Settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click reveal config vars to see that Heroku auto generated a DATABASE_URL for you. Then add production as a value with NODE_ENV as a key to the config variables as well so your code base knows that this is the production environment (remember on your local machine your NODE_ENV environment variable is set to development).&lt;/p&gt;

&lt;p&gt;Once this is all done you can click &lt;code&gt;more&lt;/code&gt; in the top right, and click &lt;code&gt;run console&lt;/code&gt; to open a console command box. Where it says &lt;em&gt;bash&lt;/em&gt; you run your &lt;code&gt;knex migrate:latest&lt;/code&gt; and &lt;code&gt;knex seed:run&lt;/code&gt; commands. This will migrate and seed your Heroku database.&lt;/p&gt;

&lt;p&gt;If your migration was successful you'll see this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75xvbkxvosqscxqc7kg8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75xvbkxvosqscxqc7kg8.png" alt="after migration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And after successful seeding you'll see this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vovhur1q5lordd5am3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vovhur1q5lordd5am3x.png" alt="after seeding"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Connecting with Code
&lt;/h1&gt;

&lt;p&gt;Congrats! We now have a database on Heroku. Now how do we communicate with it? I will build the most basic model and route I can to demonstrate. We have to build out models and routes to communicate with our database using code. To communicate with the database we need to create a database configuration file. In the &lt;code&gt;data&lt;/code&gt; directory create a file called &lt;code&gt;db-config.js&lt;/code&gt; and paste this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config();

const knex = require('knex');
const dbEnvironment = process.env.NODE_ENV || 'development';
const configs = require('../../knexfile')[dbEnvironment]

module.exports = knex(configs)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code figures out what database environment we are using based on our NODE_ENV environment variable and sets it to &lt;code&gt;dbEnvironment&lt;/code&gt; and then uses our configuration from our &lt;code&gt;knexfile.js&lt;/code&gt; for that environment and sets it to a &lt;code&gt;configs&lt;/code&gt; variable. We are using either development or production in this project. Then it exposes that database configuration so that our code can query the correct database and preform basic operations within our app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp48jsr4o43hfousyvo1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp48jsr4o43hfousyvo1v.png" alt="db config file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once our db config file is set up we can create a &lt;code&gt;model.js&lt;/code&gt; and &lt;code&gt;route.js&lt;/code&gt; file at the root of the project. Normally I would put model and route files within an &lt;code&gt;API&lt;/code&gt; folder but for the sake of speed and simplicity I will put them in the main project folder. Open the newly created &lt;code&gt;model.js&lt;/code&gt; and add this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const db = require('./data/db-config');

const findAll = () =&amp;gt; {
    return db('profiles')
}

module.exports = {
    findAll
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we are taking our database configuration, and then creating a model function &lt;code&gt;findAll&lt;/code&gt; that returns all entries of the profiles table we created. Lastly we export the model function so our routes can use it. &lt;/p&gt;

&lt;p&gt;Then we open our &lt;code&gt;route.js&lt;/code&gt; file and add a basic route to return this database information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = require('express').Router()
const Profiles = require('./model')

router.get('/', async (req, res) =&amp;gt; {
    const profiles = await Profiles.findAll()
    res.json(profiles)
})

module.exports = router
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code uses express to create a router, and pulls our model function from the model function file. Then it creates a route that uses a GET request that responds with our profile information from the database. Your model and route file should look like this if you are following this guide exactly. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yvsoidsxwovjhg46lx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yvsoidsxwovjhg46lx9.png" alt="model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosyf3ksnzcgtb2efw062.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosyf3ksnzcgtb2efw062.png" alt="route"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have your &lt;code&gt;db-config.js&lt;/code&gt;, &lt;code&gt;model.js&lt;/code&gt;, and &lt;code&gt;route.js&lt;/code&gt; all set up, the last thing to do is connect it to your &lt;code&gt;index.js&lt;/code&gt; file and test it&lt;/p&gt;

&lt;p&gt;add a few lines to your &lt;code&gt;index.js&lt;/code&gt; to make this work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const testRouter = require('./route')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This imports our route from the &lt;code&gt;route.js&lt;/code&gt; file we just worked on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.use('/test', testRouter)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells our server to use that router and allow us to access it with the '/test' route.&lt;/p&gt;

&lt;p&gt;All of this should make your &lt;code&gt;index.js&lt;/code&gt; look like this when its properly updated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdpr6hvofmabjkwbxus5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdpr6hvofmabjkwbxus5.png" alt="index updated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once all this code as been done save it all and lets go test it! &lt;/p&gt;

&lt;h1&gt;
  
  
  Test it
&lt;/h1&gt;

&lt;p&gt;Open up your internet browser and go to your site. My local site is currently at &lt;code&gt;http://localhost:5000&lt;/code&gt;. To access the data using the new code we just wrote, add your /test route at the end of the URL. &lt;code&gt;http://localhost:5000/test&lt;/code&gt;. If done correctly your web browser should now be showing you the information from your database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gv9vlvg17jhzq2adnpe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gv9vlvg17jhzq2adnpe.png" alt="localHostTest"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Personally I prefer postman for testing my APIs as it gives a clean interface to test your site. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgmlucp10r152rnrv58i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgmlucp10r152rnrv58i.png" alt="Postman"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are interested in postman and want to test your API in a dedicated application rather than your browser, &lt;a href="https://www.postman.com/downloads/" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is a link to download it. &lt;/p&gt;

&lt;h1&gt;
  
  
  Wrapping Up
&lt;/h1&gt;

&lt;p&gt;To round out this guide I am going to push all my code to Github and Heroku to make sure everything is up to date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m 'last commit'
git push
git push heroku main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now my GitHub repo has the most up to date code, and Heroku is up to date with the Git Repo. I was able to verify it by going to my new production site at &lt;a href="https://production-db-tutorial.herokuapp.com/test" rel="noopener noreferrer"&gt;https://production-db-tutorial.herokuapp.com/test&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ppmjfb2ca9gwxeas1wu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ppmjfb2ca9gwxeas1wu.png" alt="Production URL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that Concludes this guide! We started with absolutely nothing, created a new project, configured a development and production database, and built a basic model and route to interface with that database from our back end code. I hope you find this useful to get up and running with Postgres and Heroku.&lt;/p&gt;

</description>
      <category>heroku</category>
      <category>postgres</category>
      <category>database</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Working with a non profit organization to build an internal monitoring system</title>
      <dc:creator>Trenten Grede</dc:creator>
      <pubDate>Fri, 28 May 2021 01:34:31 +0000</pubDate>
      <link>https://dev.to/tgrede/working-with-a-non-profit-organization-to-build-an-internal-monitoring-system-47e6</link>
      <guid>https://dev.to/tgrede/working-with-a-non-profit-organization-to-build-an-internal-monitoring-system-47e6</guid>
      <description>&lt;h3&gt;
  
  
  Developing a full stack application for Family Promise from the back end perspective
&lt;/h3&gt;

&lt;p&gt;~     &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a New System
&lt;/h3&gt;

&lt;p&gt;I spent the entire month of May 2021 working on an internal monitoring and evaluation system for the non-profit organization &lt;a href="https://familypromise.org/"&gt;Family Promise&lt;/a&gt;. Family Promise works with homeless and low income families to help them achieve sustainable independence. They provide various prevention programs to help keep low income families from becoming homeless, as well as sheltering programs for the homeless, and aftercare programs for those who get out of sheltering. These programs are great and I personally love what the organization is doing to help the communities who need it most. That is why I am excited that I had the opportunity to work all month with a team of skilled developers. We spent a lot of time collaborating on building out a new Internal Monitoring and Evaluation System for Family Promise. This team was comprised of Myself, and 7 other Developers. We all reported to a Team Project Lead (TPL) for a daily stand-up meeting to talk about our days work. We all worked great together and its amazing what things can be done when you put different brains together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Back End
&lt;/h3&gt;

&lt;p&gt;This application is meant to be an internal monitoring system for family promise. Our goal was to allow administrative staff at family promise to get useful metrics from the data they track within the organization. We also wanted this data to be tracked and entered by service workers in the field. For example, when a service worker goes to a recipients home to provide a service they can take various notes about the service, and record useful information like location, age, household income/size, and more, so we can get metrics later on.&lt;/p&gt;

&lt;p&gt;So what? Why is this useful? Because Family Promise can use these metrics to track services that they offer to their recipients, and they can see helpful information about organizational spending on services. So what we wanted to build out is a large database that can account for many different types of metrics from the data provided by service workers. Unfortunately the original database did not have all the data necessary for proper long term metrics, so our solution was to extend the database wherever we could to account for the necessary data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our Thought Process
&lt;/h3&gt;

&lt;p&gt;When I work on a database the first thing I do is parse it into something like &lt;a href="https://www.dbdesigner.net/"&gt;DB Designer&lt;/a&gt; or &lt;a href="https://drawsql.app/"&gt;DrawSQL&lt;/a&gt;. This helps me identify the missing data in the database. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nnxtC2th--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihrshim3pu6pv8kf7k0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nnxtC2th--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihrshim3pu6pv8kf7k0k.png" alt="Original Database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I brought this to the rest of the team outlining the missing data, as well as presenting a new tentative database design. We knew we had to extend the database to account for a lot more data than we had. For example we knew we needed to track recipients, and the households they are in. This would be used in conjunction with locations and incomes to find out how Family Promise as an organization is spending money and providing services to clients. We knew a database rewrite would be costly in terms of time and we estimated that it would take us 3 to 4 days given that we would have to rewrite various model functions and routes to match with the new database tables. However, this did not turn out to be true because we found bugs in our model functions that we rewrote. Ultimately my team member Remy suggested we cascade our updates and deletes in our migration files to avoid the bugs we were running into.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fByVyZVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzi4u4tvo9tsu8wlwz0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fByVyZVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzi4u4tvo9tsu8wlwz0l.png" alt="Cascade in the code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This solved our bug for the time being but was not a permanent solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solving our Problems
&lt;/h3&gt;

&lt;p&gt;We broke down our tasks on Trello. Trello allowed us to break down our tasks and assign them to each teammate. We also used Whimsical, a design tool, to mock up the front end. We worked amongst ourselves pair programming where necessary to make the new database design happen. I used DB Designer to iterate on the original database over the course of the month. Our final schema for the database, depicted here, shows a lot more of data missing from the original database&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uRWZMMfh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8i0pkvwajc3miudci19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uRWZMMfh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8i0pkvwajc3miudci19.png" alt="Final Database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While this schema is functional and has a lot of forethought for the future of this application, its not without its own problems. As I said earlier any update or delete is set to cascade through the whole database. In the long term, we probably would not want that on every table it was only used to bypass a bug. Also when creating the migrations I had to rename tables and columns to make them less vague. This included changing file names which was throwing an error on migration. I collaborated with my teammates Remy and Diego to try to troubleshoot this issue and after several hours I stumbled across a &lt;a href="https://stackoverflow.com/questions/37970743/postgresql-unique-violation-7-error-duplicate-key-value-violates-unique-const"&gt;stack overflow&lt;/a&gt; post which had a perfect answer to our problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bq_NOiim--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciyvoxauwq3q48y9qp2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bq_NOiim--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciyvoxauwq3q48y9qp2p.png" alt="Stack Overflow Answer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we found out that when the seed files were created, they were created with a primary key. This was messing up the auto incrementing within Postgres and the indexes did not match. Our solution was to simply remove those primary key lines within the seed files. All worked well and our database was up and running.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Result
&lt;/h3&gt;

&lt;p&gt;We came out with a database that was migrated and seeded. This was good because it now allows us to just simply build model functions on top of it. This new database design holds all the data we need and now just requires all the new models and routes to be built. Over the next week and a half after the database was updated we worked on building out more of the functionality and getting it all connected to the front end forms built by the rest of the team. We estimate that this complex database design will cause some issues down the road for future teams simply because the complexity of it makes the model functions harder to write. However, we did this in hopes of preventing future rewrites from happening after model functions were already built out. I documented it thoroughly and left the design plans for the next teams that take over after us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cuj51tk3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a3n0d4nyiio0ggnre1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cuj51tk3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a3n0d4nyiio0ggnre1b.png" alt="Whimsical plan the next team"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaways?
&lt;/h3&gt;

&lt;p&gt;This has been a wonderful experience working with a team building out this system for Family Promise. I feel like this project has made me far more organized as a developer. I now understand the importance of effective communication and documentation. Without proper documents to pass on to future developers your code will not be as easily understood. I felt that there were times where certain bugs could have been avoided with just a little more communication. This point about ensuring good communication with future teams is a key take away for me. This month has taught me so much and I feel my database design skills have improved a lot since the start of my time in labs. I know that this new practice will help me in my future career as a developer. This project has further driven my passion for back end development and I am forever grateful to Family Promise and Lambda School Labs for having given me this opportunity. I can't wait to work on more projects and can't wait to see what interesting work I can do next! &lt;/p&gt;

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