Getting started with PostgreSQL on MAC

Step-1: Installation

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


$ brew install postgresql


postgresql_install_kodefork.png


Step-2: Start PostgreSQL Server

$ brew services start postgresql

start_postgres_kodefork.png


$ brew services stop postgresql


Step-3: Interactive Session PSQL

$ psql postgres

enter_psql_kodefork.png


\q


Step-4: List Databases

\l

list_databases_psql_kodefork.png



Step-5: Create Database

CREATE DATABASE testdb;

create_psql_db_kodefork.png

DROP DATABASE testdb;


Step-6: Create User for Database

CREATE USER testuser WITH PASSWORD 'password';
ALTER ROLE testuser SET client_encoding TO 'utf8'; 
ALTER ROLE testuser SET default_transaction_isolation TO 'read committed'; 
ALTER ROLE testuser SET timezone TO 'Asia/Kolkata';

GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;

set_psql_db_user_kodefork.png


final_database_list_psql_kodefork.png


Step-7: Switch Database

\c testdb

connect_psql_database_kodefork.png


Step-8: Listing Tables

\dt

tables_psql_db_kodefork.png


Step-9: Create Table

CREATE TABLE person (id INTEGER PRIMARY KEY, name VARCHAR, email VARCHAR);

tables_psql_kodefork.png

Alternatively, if you want you can drop table using

DROP TABLE person;


Step-10: Fetching Records from Table

SELECT * FROM person;

record_psql_table_kodefork.png


Step-11: Inserting Data in Table

INSERT INTO person(id, name, email) VALUES(1, 'astik anand', 'astikanand@gmail.com');

insert_record_psql_table_kodefork.png

Step-12: Backup Database

$ pg_dump testdb > backup

dump_psql_kodefork.png


Step-13: Restore Database

$ psql newtestdb < backup