Wednesday, 13 July 2016

postgreSQL what is schema?

Schema is nothing but user ownes 
  • Tables
  • Views
  • Sequences
  • Functions
  • Domains
  • Packages
  • Procedures
  • Synonyms
  • Database Links
  • Procedures

Benefits of Schems:

  • A database can contains one or more named sachems.
  • By default, all databases contain a public schema.
There are several reasons why one might want to use sachems:
  • To allow many users to use one database without interfering with each other.
  • To organize database objects into logical groups to make them more manageable.
  • Third-party applications can be put into separate schemas so they cannot collide with the names of other objects.

Schema search path:

  • Qualified names are tedious to write, so we use table names directly in queries.
  • If no schema name is given, the schema search path determines which schemas are searched for matching table names
Example:
SELECT * FROM employee
This statement will find the first employee table from the schemas listed in the search path.

  • The first schema named in the search path is called the current schema if that named schema exists.
  • Aside from being the first schema searched, it is also the schema in which new tables will be created if the CREATE TABLE command does not specify a schema name.
  • To show the current search path, use the following command:
            SHOW search_path;
  • To put our new schema in the path, we use:
        SET search_path TO myschema, public;
  • The first element specifies that a schema with the same name as the current user is to be searched. If no such schema exists, the entry is ignored.
  • The second element refers to the public schema that we have seen already. To put our new schema in the path, we use:

SET search_path TO myschema, public;

Default Schema:
postgres=# show search_path;
search_path
----------------
"$user",public
(1 row)
postgres=#

To put our new schema in the path, we use:
SET search_path TO myschema, public;

Example:

postgres=# set search_path TO myschema,public;
SET
postgres=# create table cust(id integer, name varchar(10));
CREATE TABLE
postgres=# \d
List of relations
Schema | Name | Type | Owner
----------+--------------------------+----------+----------
myschema | cust | table | postgres
public | consultant | table | postgres
public | consultant_id_sequence | sequence | postgres
(3 rows)
postgres=# set search_path to "$user",public;
SET
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------------------------+----------+----------
public | consultant | table | postgres
public | consultant_id_sequence | sequence | postgres
(2 rows)



also see below

postgreSQL - PSQL MATA Commands


PSQL META CommandUsage
\dtList the table
\dnList the sachems
\lList the databases
\diList of Indexes
\d <table-name>Describe table(Columns and their type)
\dbList of table spaces(In which places tables are storing)
\duList of users
\dsList of system table
\dvsList of system views
\l+List of databases with size
\dt+List of tables with size
\!present working directory
\dpTable space privileges 
\qQuit from psql shell
select current_userTo view current user
How to switch to database/user?
\c <db> <user>

how to create database outside?
createdb -p 5444 <db-name> -u <user>

Note: we can refer for port in postgres.conf

How to write Query results in to file?

\o <path>
select * from <table-name>;
\o --->switching off

How to importing script?
\i <path of .sql file>

How to print time taken by query?
\timing
-->time take to run particular query
Query
\timtig -->turn off

How to write history of commands into file?
\s <path>

Alternate for \dt+

select pg_relation_size('<table-name>')
    output in bytes

select pg_size_pretty(pg_relation_size('<table-name>'))
    output in MBs or in GB

select pg_size_pretty(pg_database_size('<database-name>'))
    output in MBs or in GB

select pid from pg_stat_activity;

History of PostgreSQL

History of PostgreSQL

The world’s most advanced open source database
Designed for extensibility and customization .
Actively developed for more than 20 years
- University Postgres (1986-1993)
- Postgres95 (1994-1995)
- PostgreSQL (1996-current)

Features of PostgreSQL

Portable:

Written in ANSI C
Supports Windows, Linux, Mac OS/X and major UNIX platforms

Reliable:

  •  ACID Compliant
  •  Supports Transactions
  •  Supports Save points
  •  Uses Write Ahead Logging (WAL)

Secure:

  • Uses Multi-version Concurrency Control
  • Supports Table Partitioning
  • Supports Tablespaces

Recovery and Availability: 

  •  Streaming Replication
  •  Replication Slots
  •  Supports Hot-Backup, pg_basebackup
  •  Point-in-Time Recovery

Advanced:

  • Supports Triggers and Functions
  • Supports Custom Procedural Languages PL/pgSQL, PL/Perl, PL/TCL, PL/PHP, PL/Java - Upgrade using pg_upgrade
  • Unlogged Tables - Materialized Views


Why postgresSQL?

Why postgresSQL?


PostgreSQL is capable of processing big data and which has better capabilities as follows.

LimitValue
Maximum Database SizeUnlimited
Maximum Table Size32 TB
Maximum Row Size1.6 TB
Maximum Field Size1 GB
Maximum Rows per Table Unlimited
Maximum Columns per Table250-1600 (Depending on Column types)
Maximum Indexes per Table?Unlimited

what is postgreSQL

What is postgreSQL?


PostgreSQL is a Relational Database Management System(RDBMS) is a named collection of SQL objects. It is a collection of schemas and the schemas contain the tables, functions, etc.
  • Databases are created with the CREATE DATABASE command and
  • Databases are destroyed with the DROP DATABASE command.
To determine the set of existing databases:
-SQL: SELECT datname FROM pg_database;
Above command will list all the databases
-psql META COMMAND: \l(backslash lowercase L)
Using \l also we can list all the databases

Tuesday, 12 July 2016

How to check disk space in linux

Example:
[pgdb@localhost home]$ df -H
Filesystem             Size   Used  Avail Use% Mounted on
/dev/vda3              106G    12G    89G  12% /
tmpfs                   22G    78k    22G   1% /dev/shm
/dev/vda1              204M    34M   160M  18% /boot
/dev/vda2              201G   136G    55G  72% /data1
/dev/vda6              201G   402M   191G   1% /data2
172.19.1.25:/data/pub
                       316G   216G    84G  73% /nfs

How to check disk space of a folder?
Example:
[pgdb@localhost home]$ df -H /home
Filesystem             Size   Used  Avail Use% Mounted on
/dev/vda3              106G    12G    89G  12% /

Get more about df command using below?
df --help
or
man df