Learn how to use PostgreSQL with Node JS using express server with JWT authentication.
- Node & NPM ⬇️ - (node v16), (npm v8)
- PostgreSQL ⬇️ - (v14)
Ubuntu:
sudo -i -u postgres
Windows:
psql -U postgres
CREATE USER mahmoud WITH PASSWORD 'secret' SUPERUSER;
CREATE DATABASE dailynews
OWNER mahmoud
ENCODING UTF8;
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;