Skip to main content

POSTGRES COMMANDS

 1. Create your own database on provided server. Name it DB_yourname.
 
 postgres=# create user db_rahul with password 'abcd';
CREATE ROLE
postgres=# 
postgres=# 
postgres=# create database db_rahul with owner db_rahul;
CREATE DATABASE
postgres=# 
 
2. Create table, where you have 3 columns:

'ID', 'Name', 'Organization', 'department'

Fill in this table with data - create 3 records.
 
 
 postgres=# \c db_rahul
You are now connected to database "db_rahul" as user "postgres".
db_rahul=# create table customer ( id int not null,
db_rahul(# name text not null,
db_rahul(# organization text not null,
db_rahul(# department text not null );
CREATE TABLE
db_rahul=# insert into customer (id,name,organization,department) values (1,'rahul','datacom','Cloud');
INSERT 0 1
db_rahul=# insert into customer (id,name,organization,department) values (2,'vishnu','datacom','Cloud');
INSERT 0 1
db_rahul=# insert into customer (id,name,organization,department) values (3,'ranjith','datacom','Cloud');
INSERT 0 1
db_rahul=# insert into customer (id,name,organization,department) values (4,'sreejith','syntrio','Level 3');
INSERT 0 1
db_rahul=# select * from customer;
 id |   name   | organization | department 
----+----------+--------------+------------
  1 | rahul    | datacom      | Cloud
  1 | vishnu   | datacom      | Cloud
  1 | ranjith  | datacom      | Cloud
  4 | sreejith | syntrio      | Level 3
(4 rows)

 
 
 
 
 3. Mark 'ID' column as 'Index' in your table.
 
 db_rahul=# create index id_number on customer(id);
CREATE INDEX
db_rahul=# \d customer;
      Table "public.customer"
    Column    |  Type   | Modifiers 
--------------+---------+-----------
 id           | integer | not null
 name         | text    | not null
 organization | text    | not null
 department   | text    | not null
Indexes:
    "id_number" btree (id)
 
 
 
 
 4. Create another table where you have 4 columns:

'ID', 'Name', 'City', 'Street'
 
 db_rahul=# create table sales ( id int not null,
name text not null,
city text not null,
street text not null );
CREATE TABLE
db_rahul=# insert into sales (id,name,city,street) values (1,'rahul','selangor','kaula lumpur');
INSERT 0 1
db_rahul=# insert into sales (id,name,city,street) values (2,'vishnu','selangor','kaula lumpur');
INSERT 0 1
db_rahul=# insert into sales (id,name,city,street) values (3,'sreejith','kilimanor','trivandrum');
INSERT 0 1
db_rahul=# select * from sales;
 id |   name   |   city    |    street    
----+----------+-----------+--------------
  1 | rahul    | selangor  | kaula lumpur
  2 | vishnu   | selangor  | kaula lumpur
  3 | sreejith | kilimanor | trivandrum
(3 rows)
 
 5. Create a foreign key that will connect 'ID' from 1st table with ID from 2nd table.
 
 db_rahul=# alter table customer add constraint id_detail unique (id);
ALTER TABLE
db_rahul=# alter table sales add FOREIGN KEY (id) references customer (id) on update cascade;
ALTER TABLE
db_rahul=# alter table sales add constraint id_details unique (id);
ALTER TABLE

db_rahul=# \d sales;
     Table "public.sales"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
 name   | text    | not null
 city   | text    | not null
 street | text    | not null
Indexes:
    "id_details" UNIQUE CONSTRAINT, btree (id)
Foreign-key constraints:
    "sales_id_fkey" FOREIGN KEY (id) REFERENCES customer(id) ON UPDATE CASCADE

db_rahul=# \d customer;
      Table "public.customer"
    Column    |  Type   | Modifiers 
--------------+---------+-----------
 id           | integer | not null
 name         | text    | not null
 organization | text    | not null
 department   | text    | not null
Indexes:
    "id_detail" UNIQUE CONSTRAINT, btree (id)
    "id_number" btree (id)
Referenced by:
    TABLE "sales" CONSTRAINT "sales_id_fkey" FOREIGN KEY (id) REFERENCES customer(id) ON UPDATE CASCADE



 
 
 
 
 6. Try to test that it is working: try to update ID of the 1st or 2nd table (or try INSERT statement).
 You should get error.
 
 db_rahul=# update sales set id=6 where name='vishnu';
ERROR:  insert or update on table "sales" violates foreign key constraint "sales_id_fkey"
DETAIL:  Key (id)=(6) is not present in table "customer".
 
 
 
 
 7. Compose query that will show you all names from 1st table where column 'City' from 2nd table = Kuala Lumpur (or any other you have in your 2nd table) - use operator INNER JOIN. 
 
 
 db_rahul=# select * from customer inner join sales on (customer.name=sales.name) where street='kaula lumpur'
;
 id |  name  | organization | department | id |  name  |   city   |    street    
----+--------+--------------+------------+----+--------+----------+--------------
  1 | rahul  | datacom      | Cloud      |  1 | rahul  | selangor | kaula lumpur
  2 | vishnu | datacom      | Cloud      |  2 | vishnu | selangor | kaula lumpur
(2 rows)
 db_rahul=# select customer.name,sales.city from customer inner join sales on (customer.name=sales.name) where street='kaula lumpur'
;
  name  |   city   
--------+----------
 rahul  | selangor
 vishnu | selangor
(2 rows)
 
8. Compose query that will show you 'Organization' details from 1st table together with 'City', 'Street' from 2nd table. 
 
 db_rahul=# select customer.organization,sales.city,sales.street from customer full outer join sales on (customer.name=sales.name) ;
 organization |   city    |    street    
--------------+-----------+--------------
 datacom      | selangor  | kaula lumpur
 syntrio      | kilimanor | trivandrum
 datacom      | selangor  | kaula lumpur
 datacom      |           | 
(4 rows)
 
 
 
 
 Database administration task.
 
 
to copy the schema of a table
 
 
db_rahul=# select * into salesnew from sales where 1=2;
SELECT 0
db_rahul=# select * from sales;
 id |   name   |   city    |    street    
----+----------+-----------+--------------
  1 | rahul    | selangor  | kaula lumpur
  2 | vishnu   | selangor  | kaula lumpur
  3 | sreejith | kilimanor | trivandrum
(3 rows)

db_rahul=# select * from salesnew;
 id | name | city | street 
----+------+------+--------
(0 rows)

 
 
 
 
 
 
 
 
 
 
 1. Create a backup of your database. What is the size of it? Restore it to another name,
 
 
 postgres@rahul-Inspiron-3542:~$ pg_dump db_rahul > db_rahul
postgres@rahul-Inspiron-3542:~$ du -sch db_rahul
4.0K db_rahul
4.0K total
postgres@rahul-Inspiron-3542:~$ psql
psql (9.4.5)
Type "help" for help.

postgres=# create database db_rahulnew;
CREATE DATABASE
postgres=# \q
postgres@rahul-Inspiron-3542:~$ psql db_rahulnew < db_rahul 
SET
SET
SET
SET
SET
SET
CREATE EXTENSION
COMMENT
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
COPY 4
COPY 3
COPY 0
ALTER TABLE
ALTER TABLE
CREATE INDEX
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
postgres@rahul-Inspiron-3542:~$ psql
psql (9.4.5)
Type "help" for help.

postgres=# \c db_rahulnew;
You are now connected to database "db_rahulnew" as user "postgres".
db_rahulnew=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | customer | table | postgres
 public | sales    | table | postgres
 public | salesnew | table | postgres
(3 rows)
 
 
 
 
 
 
 

Comments

Popular posts from this blog

SystemD commands

[root@centos7 ~]# systemctl -t target UNIT                   LOAD   ACTIVE SUB    DESCRIPTION basic.target           loaded active active Basic System cryptsetup.target      loaded active active Encrypted Volumes getty.target           loaded active active Login Prompts graphical.target       loaded active active Graphical Interface local-fs-pre.target    loaded active active Local File Systems (Pre) local-fs.target        loaded active active Local File Systems multi-user.target      loaded active active Multi-User System network-online.target  loaded active active Network is Online network.target         loaded active active Network nfs-client.target      loaded active active NFS client services nss-user-lookup.target loaded active active User and Gr...

How to tweak linux server harddisk using hdparm

hdparm switches explained http://manpages.ubuntu.com/manpages/intrepid/man8/hdparm.8.html   First of all you have to install hdparm in linux. apt-get install hdparm #hdparm /dev/sda /dev/sda: readonly = 0 (off) readahead = 120 (on) geometry = 8850/255/63, sectors = 142182912, start = 0 Hard disk Performance Information # hdparm -tT /dev/hda /dev/hdd: Timing cached reads: 496 MB in 2.00 seconds = 247.42 MB/sec Timing buffered disk reads: 60 MB in 3.03 seconds = 19.81 MB/sec Hard drive set to low, slow settings # hdparm -cuda /dev/hda /dev/hda: IO_support = 0 (default 16-bit) unmaskirq = 0 (off) using_dma = 0 (off) readahead = 256 (on) Use below tweaks to increase disk read write performance. For sda drive ~]# hdparm -a 2048 /dev/sda /dev/sda: setting fs readahead to 2048 readahead = 2048 (on) For sdb drive [root@439298a ~]# hdparm -a 2048 /dev/sdb /dev/sdb: setting fs readahead to 2048 readahead = 2048 (on) ]# echo “anticipatory” >...

RAID

Check the Raid installed lspci | grep RAID     Software Raid ============== Linux Support For Software RAID Currently, Linux supports the following RAID levels (quoting from the man page): LINEAR RAID0 (striping) RAID1 (mirroring) RAID4 RAID5 RAID6 RAID10 MULTIPATH, and FAULTY. MULTIPATH is not a Software RAID mechanism, but does involve multiple devices: each device is a path to one common physical storage device. FAULTY is also not true RAID, and it only involves one device. It provides a layer over a true device that can be used to inject faults. Install mdadm Type the following command under RHEL / CentOS / Fedora Linux: # yum install mdadm Type the following command under Debian / Ubuntu Linux: # apt-get update && apt-get install mdadm How Do I Create RAID1 Using mdadm? Type the following command to create RAID1 using /dev/sdc1 and /dev/sdd1 (20GB size each). First run fdisk on /dev/sdc and /dev/sdd with " Softwa...