/expresspg

Primary LanguageTypeScript

Express & PostgreSQL 🐘 + JWT Auth

JWT

Learn how to use PostgreSQL with Node JS using express server with JWT authentication.

Requirments

  1. Node & NPM ⬇️ - (node v16), (npm v8)
  2. PostgreSQL ⬇️ - (v14)

Create Database & User

Command Line Access

Ubuntu:

sudo -i -u postgres

Windows:

psql -U postgres

Create A User

Docs

CREATE USER mahmoud WITH PASSWORD 'secret' SUPERUSER;

Create A Database

Docs

CREATE DATABASE dailynews
    OWNER mahmoud
    ENCODING UTF8;

db-migrate commands

Create Migration:

db-migrate create posts --sql-file

Run Migrations:

db-migrate up

Reset Migrations:

db-migrate reset

Example migration up sql file:

CREATE TABLE posts (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL UNIQUE,
    body TEXT NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

Example migration down sql file:

DROP TABLE posts;