On your root directory create a directory called db
.
Inside of the db
directory touch a file called schema.sql
.
In the schema.sql
file create a squema.
E.g.:
CREATE DATABASE my_database_name;
CREATE TABLE my_table
(
id SERIAL PRIMARY KEY,
content TEXT NOT NULL
);
INSERT INTO my_table
(content)
VALUES
('random text');
note: A database schema represents the logical
configuration of all or part of a relational database.
It can exist both as a visual representation and as
a set of formulas known as integrity constraints
that govern a database.
$ psql
$ CREATE DATABASE my_database;
$ \c my_database
$ CREATE TABLE my_table
(
id SERIAL PRIMARY KEY,
content TEXT NOT NULL
);
$ INSERT INTO my_table
(content)
VALUES
('random text');
$ \dt
E.g. result:
Schema | Name | Type | Owner |
---|---|---|---|
public | my_table | table | me |
$ select * from my_table;
E.g. result:
id | content |
---|---|
1 | random text |
web: npm start
node_modules
config/config.env
config/db.js
const { Pool } = require('pg')
const dotenv = require('dotenv')
dotenv.config({ path: './config/config.env' })
// === PRODUCTION === //
const isProduction = process.env.NODE_ENV === 'production'
const productionDB = process.env.DATABASE_URL
// === DEVELOPMENT === //
const developmentDB = `postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_DATABASE}`
// === DB CONNECTION === //
const pool = new Pool({
connectionString: isProduction ? productionDB : developmentDB,
ssl: isProduction,
})
$ heroku login
$ heroku create
Use the heroku addons command to determine whether your app already
has Heroku Postgres provisioned:
$ heroku addons
Add-on Plan Price State
────────────────────────────────────────────────────────── ───────── ───── ───────
heroku-postgresql (postgresql-concave-52656) hobby-dev free created
If heroku-postgresql doesn’t appear in your app’s list of add-ons,
you can provision it with the following CLI command:
$ heroku addons:create heroku-postgresql:hobby-dev
note: hobby-dev is a free database plan offered by Heroku.
As part of the provisioning process, a DATABASE_URL config var
is added to your app’s configuration. This contains the URL
your app uses to access the database.
$ git add .
$ git commit -m "message..."
$ git push heroku master
$ git clone https://github.com/PhillipeAlves/node_pg_api.git cd node_pg_api
$ npm install
$ npm run dev