Postgres SQL Command Sheet
Postgres SQL Command Sheet
BY CMD
Firstly, add the path of postgres sql in system environment.
Then use the command psql -U postgres.
IN LINUX ENVIRONMENT
apt install postgresql
must be run as sudo
sudo -iu postgres(as a postgres user)
psql (to inter in postgres).
Commands
\l or \list to check the pre-build data base.
\! cls to clear the screen.
create database (database name)db ; to create the new
database.
\list to verify the existence of database.
\q exit to postgres.
exit back to kali.
\c (database name)db; to change the database.
drop database (database name); to delete data base.
Decimal data type:
CRUD: e.g. create table Abdul(
id decimal (6,4),
Creating table:
name VARCHAR(255)
e.g
);
create table (database name)( Decimal represent data type, total number
id int , will be 6 digit long and after decimal there
name VARCHAR (input size), will be four numbers.
course VARCHAR (input size)
);
Constraints:
Primary key;
It uniquely identify each table it should be different through
\d (table name); to check the table specifications.
out the table, e.g. id or user name.
Inserting data in table:
Id primary key, or user name primary key,
insert into (table name) (attributes, e.g. ID, name,
Not null:
description)
e.g. create table students(
Values
id int,
(1, ‘Abdul’, ‘Software Engineer);
name VARCHAR not null,
Reading from table:
);
select * from (table name);
It’s means that in name column there should not be a null
it will show the full table, but if you wanna read only
value.
specific column then you can use the command;
Default value:
e.g. I wanna read only the name column from the desired
e.g. create table customer (
table,
id int primary,
select name (table name); or name VARCHAR(255) not null,
select id (table name); to read the id column from the acc_description not null default ‘saving’
table. );
Updating the table: It will be save ‘saving’ by default in the acc_description
update (table name) column if you leave blank.
set description = ‘Pentester’
where id=1;
By this in table the description for id 1 will change to
pentester.
Delete data from table:
delete from (table name)
where ID=1;
It will delete the id 1 data from the table.