Wednesday 13 July 2016

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;

No comments:

Post a Comment