Setup Database
- This assumes that you already have a postgres database installed
Create database
Connect to postgres with psql
psql -U postgres
Run create database command
create database dev_demo_db;
Exit psql
\q
Enter connection string into .env file
- Any special characters in password will need to be url encoded
- @ = %40
DATABASE_URL=postgres://postgres:<password>@localhost:5432/dev_demo_db
Setup database driver
Install pg database driver
npm install pg
Create database module
- This is the module that will be used in your application app code
Create db.js file
touch src/db/index.js
Code a shared connection pool
import pg from 'pg';
const { Pool } = pg;
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
export default pool;
Commit
git add .
git commit -m 'Adds db index file'
Next:
Setup Migrations