Introduction to PostgreSQL

 Introduction to PostgreSQL

CREATOR: Amruta Ashish Pednekar

What is PostgresQL ?



Database


For

 

Storing Data

Database system which stores data into tables for easy access

Install PostgresQL 

  • Go to https://www.postgresql.org/download/

  • Select your opening system family

  • Download installer

  • Click on downloaded installer and run as administrator

  • Enter admin password. It will start installation.

  • Click on NEXT button after every step

  • It will ask you for a password for database superuser. Enter and note is somewhere for future use

  • Check below option

  • Finish installation

Starting PGAdmin 

  • Go to path where you installed PostgreSQL

  • Go to <version> -> PGAdmin -> bin

  • Open pgAdmin4

  • Set a master password for pgAdmin

  • Connect to server

    • Go to the left panel. Click on to servers

    • Select server . Its PostgreSQL<version>

    • Enter the same password you set while PostgreSQL installation

    • Once you are connected to server, you can see other dropdowns like Tablespaces

  • If you face any error while connecting postgres server on windows, go to service manager and start postgresql 

    • run -> services.msc -> find postgresql-x64-10 -> start the service


Create table

Using pgAdmin4 UI

  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables 
  • Right click and Create -> Table
  • Create and save
  • Create columns
    • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables ->  <Table name>
    • Right click and Create -> Column
    • Add column name and data type
    • Save

Using Query Tool

  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables
  • Right click -> Query Tool
  • Write create table query in Query Editor
    • create table table_name( column_name1 data_type, column_name2 data_type);
    • Execute 
    • Refresh Tables from left panel 

Drop table

  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables
  • Right click -> Query Tool
  • Write create table query in Query Editor
    • drop table if exists table_name;
  • It drops a table from the database if it exists and skips if table doesn't exist 

Alter table

  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables
  • Right click -> Query Tool
  • Write create table query in Query Editor 
    • create table table_name
    • alter table table_name add column “column_name1” data_type,”column_name2” data_type;
    • alter table table_name drop column “column_name”;

Run commands from sql file

  • Create a new file with extension .sql
  • Write sql commands
  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables
  • Right click -> Query Tool  (Query Editor) -> Open file 
  • Execute (It executes all commands listed inside this file)

Copying CSV records into table

  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables
  • Right click -> Query Tool  (Query Editor)
  • COPY table_name(column_name1, column_name2, column_name3, column_name4)
      FROM 'C:\data.csv'
      DELIMITER ','
      CSV HEADER;
  • FROM shows csv file path
  • If there is space in between column names , 
    • Add double quotes. E.g. “Item name”
    • Single quote will not work here. It gives syntax errors.
  • Execute 
  • Refresh Tables from left panel

Using pgAdmin4 UI

  • Go to pgAdmin left panel -> Servers -> PostgreSQL -> Databases -> Schemas -> Tables 
  • Right click to table_name
  • Go to Import/ Export
  • Follow instructions and import from csv file.

Comments

  1. Congratulations for the Blog on codeing . Wish you all the bey

    ReplyDelete

Post a Comment

Popular posts from this blog

PERSONAL ACCESS TOKEN ON GITHUB

PostgreSQL Commands

Rails Application with Docker