• Bootcamp (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • Pg

    BootcampBackend2021-02-16


    Install Server

    How to Install PostgreSQL for Mac OS X

    /Library/PostgreSQL/9.6 /Library/PostgreSQL/9.6/data port 5432 Locale: en_US.UTF-8

    $ brew install postgres
    $ brew tap homebrew/services
    $ brew services start postgresql
    $ brew services stop postgresql
    $ brew services restart postgresql

    uninstall old 9.4 version

    $ sudo /Library/PostgreSQL/9.4/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
    $ sudo rm -rf /Library/PostgreSQL/9.4

    Install Client

    $ npm i -S pg

    1. create login user

    CREATE USER psql WITH
    	LOGIN
    	SUPERUSER
    	CREATEDB
    	CREATEROLE
    	INHERIT
    	NOREPLICATION
    	CONNECTION LIMIT -1
    
    GRANT postgres TO psql;

    2. create database

    CREATE DATABASE react_redux
        WITH 
        OWNER = psql
        ENCODING = 'UTF8'
        CONNECTION LIMIT = -1;
    
    COMMENT ON DATABASE react_redux
        IS 'william create DB `react_redux` under the owner `psql`';

    3. create todo table

    CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)')

    RESTFul APIs:

    Function URL Action


    CREATE /api/v1/todos Create a single todo READ /api/v1/todos Get all todos UPDATE /api/v1/todos/:todoid Update a single todo DELETE /api/v1/todos/:todoid Delete a single todo

    • CRD: work.

    improvement

    • items -> id -> integer
    CREATE SEQUENCE item_id_seq;
    ALTER TABLE items ALTER id SET DEFAULT NEXTVAL('item_id_seq');
    
    ALTER TABLE items ADD CONSTRAINT item_id_seq UNIQUE(id)
    
    SELECT setval('item_id_seq', (SELECT MAX(id) FROM items)+1);
    • duplicated sequenced-index
    • primary key / max-id

    pgadmin

    https://www.postgresql.org/ftp/pgadmin/pgadmin4/v3.1/macos/

    PostgreSQL + redux/todoMVC

    1. server-side: make PostgreSQL work

    • install PostgreSQL 9.6
    • pgAdmin4: login user: psql/psql
    • pgAdmin4: create DB: react-redux
    • bin/psql-create-table.js: items
    • bin/psql-insert-table.js: insert table items
    • node server: server/server-psql.js: webserver just for psql. server/postgr_db.js: pool server/pg/: CRUD todos
    • test: localhost:8083/api/pg/todos/ works

    2. Front-end: