DEV Community

Cover image for How to use PostgreSQL with NodeJS by SilvenLEAF
SilvenLEAF
SilvenLEAF

Posted on • Edited on

How to use PostgreSQL with NodeJS by SilvenLEAF

Ahoy there mates! Who's excited to embark onto the SQL world? Let's go meet PostgreSQL!! Stay tuned. Because on the very NEXT blog, we'll learn how to automate all DB setups and Schema generations!

On this blog, we'll learn how to setup PostgreSQL with NodeJS!! To double the fun, let's throw some TypeScript as well. Don't worry, even if you don't know it, we'll be going from level 0, so as long as you know the basics of JavaScript, hop on. Let the adventure begin!!

Step 0: Project Setup

Create a folder and open it in your favorite editor (mine VS Code). Then type this command on your project terminal

npm init -y
Enter fullscreen mode Exit fullscreen mode

(It'll create a package.json file to track all the packages that you'd download and so on)

Bonus Step: Adding TypeScript

For those who are a bit lost on how to set up the environment and run the TypeScript files, check this one out TypeScript SETUP by SilvenLEAF

Well anyway, in short (for details, checkout the above link)

  • install typescript
npm i typescript 
Enter fullscreen mode Exit fullscreen mode
  • init our tsconfig (make sure you already have typescript globally installed, if not type npm i -g typescript. And don't get it confused with the previous normal npm i typescript command)
tsc --init
Enter fullscreen mode Exit fullscreen mode

(It'll create a .tsconfig file)

  • install ts-node and ts-node-dev
npm i ts-node ts-node-dev
Enter fullscreen mode Exit fullscreen mode

Now finally, let's setup our PostgreSQL Database

Step 1: Install DB Packages

Type the following command to install the required db packages.

npm i sequelize @types/sequelize sequelize-cli pg pg-hstore
Enter fullscreen mode Exit fullscreen mode

Step 2: Create .sequelizerc file

Type touch .sequelizerc to create .sequelizerc file. Now paste the following content into this .sequelizerc file.

const path = require('path')

module.exports = {
  config: path.resolve('./database/config', 'config.js'),
  'models-path': path.resolve('./database/models'),
  'seeders-path': path.resolve('./database/seeders'),
  'migrations-path': path.resolve('./database/migrations'),
}
Enter fullscreen mode Exit fullscreen mode

Sequelize uses this .sequelizerc file to generate the config and the model using the specified path.

Next up, we generate the config by running the command mentioned in next step.


Step 3: Generate Database config

Make sure you have sequelize-cli globally installed. If not, install it globally by using this command npm i -g sequelize-cli. Otherwise the following command won't work.

Type the following command to create the Database config.

sequelize init
Enter fullscreen mode Exit fullscreen mode

It will create a folder called database on the root level and with the following structure/stuff inside it.

const database = {
  config: {
    "config.js": "it is the config js file",
  },
  migrations: {},
  models: { "index.js": null}, // here, I mean, in this "models" folder, we'll put our db models
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Edit database/config/config.js

Next up, let's edit the database/config/config.js.
Replace the content of the database/config/config.js with the code below:

const database = {
   user: 'yourDatabaseUser',
   password: 'yourDatabasePassword', 
   name: 'yourDatabaseName', 
};
const DB_STRING = `postgres://${ database.user }:${ database.password }@127.0.0.1:5432/${ database.name }`

module.exports = {
  development: {
    url: DB_STRING,
    dialect: 'postgres',
  },
  test: {
    url: DB_STRING,
    dialect: 'postgres',
  },
  production: {
    url: DB_STRING,
    dialect: 'postgres',
  },
}
Enter fullscreen mode Exit fullscreen mode

Congratulations you are all set up!! Now, create your schemas in that modals folder and export it. Then import in the file where you want to use it and use it to your heart's content!

But you know what? We are not gonna do that here now. Because We'll automate them right in the next blog. So stay tuned!

NEXT blog coming soon by Nov 3rd!!

What's NEXT?

1. Automate creating DB Schemas

2. Insane stuff with JavaScript/TypeScript

3. Debugging TypeScript with VS Code Debugger

4. How to automate anything

5. Sequelize Hooks

6. Improved AI BOT that can do anything

7. How to create an Android APP with NO XP

(including apk generating)

Got any doubt?

Drop a comment or Feel free to reach out to me @SilveLEAF on Twitter or Linkedin

Wanna know more about me? Come here!
SilvenLEAF.github.io

Top comments (0)