15 Advanced PostgreSQL Commands
15 Advanced PostgreSQL Commands
15 Advanced PostgreSQL Commands
Home
About
Free eBook
Archives
Best of the Blog
Contact
Subscribe
HowTo & FAQ
Ads by Google Postgres Database Administration Linux Commands SQL Server Database
Also, refer to our previous article about 15 Practical PostgreSQL DBA Commands.
1 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
relname | relpages
-----------------------------------+----------
pg_proc | 50
pg_proc_proname_args_nsp_index | 40
pg_depend | 37
pg_attribute | 30
If you want only the first biggest table in the postgres database then append the above query with limit as:
# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC limit 1;
relname | relpages
---------+----------
pg_proc | 50
(1 row)
If you want it to be shown pretty, then use pg_size_pretty function which converts the size in bytes to human
understandable format.
# SELECT pg_size_pretty(pg_database_size('geekdb'));
pg_size_pretty
----------------
60 MB
(1 row)
This is the total disk space size used by the mentioned table including index and toasted data. You may be
interested in knowing only the size of the table excluding the index then use the following command.
# SELECT pg_size_pretty(pg_total_relation_size('big_table'));
pg_size_pretty
----------------
55 MB
(1 row)
2 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
# SELECT pg_size_pretty(pg_relation_size('big_table'));
pg_size_pretty
----------------
38 MB
(1 row)
As shown in the example below, at the end of the output you will have a section titled as indexes, if you have
index in that table. In the example below, table pg_attribute has two btree indexes. By default postgres uses
btree index as it good for most common situations.
test=# \d pg_attribute
Table "pg_catalog.pg_attribute"
Column | Type | Modifiers
---------------+----------+-----------
attrelid | oid | not null
attname | name | not null
atttypid | oid | not null
attstattarget | integer | not null
attlen | smallint | not null
attnum | smallint | not null
attndims | integer | not null
attcacheoff | integer | not null
atttypmod | integer | not null
attbyval | boolean | not null
attstorage | "char" | not null
attalign | "char" | not null
attnotnull | boolean | not null
atthasdef | boolean | not null
attisdropped | boolean | not null
attislocal | boolean | not null
attinhcount | integer | not null
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)
5. How to specify postgreSQL index type while creating a new index on a table ?
By default the indexes are created as btree. You can also specify the type of index during the create index
statement as shown below.
3 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
All the operations performed after the BEGIN command will be committed to the postgreSQL database only you
execute the commit command. Use rollback command to undo all the transactions before it is committed.
# ROLLBACK -- rollbacks the transaction.
# COMMIT -- commits the transaction.
7. How to view execution plan used by the postgreSQL for a SQL query ?
# EXPLAIN query;
8. How to display the plan by executing the query on the server side ?
This executes the query in the server side, thus does not shows the output to the user. But shows the plan in
which it got executed.
# EXPLAIN ANALYZE query;
Following example gives the total number of rows with a specific column value is not null.
# select count(col_name) from table;
Following example displays the distinct number of rows for the specified column value.
# select count(distinct col_name) from table;
11. How can I get the second maximum value of a column in the table ?
4 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
# SELECT MAX(num) from number_table where num < ( select MAX(num) from number_table );
12. How can I get the second minimum value of a column in the table ?
# SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table );
The output of the query will be redirected to the “output_file”. After the redirection is enabled, the select
command will not display the output in the stdout. To enable the output to the stdout again, execute the \o
without any argument as mentioned below.
# \o
As explained in our earlier article, you can also backup and restore postgreSQL database using pg_dump and
psql.
PostgreSQL database can encrypt the data using the crypt command as shown below. This can be used to store
your custom application username and password in a custom table.
# SELECT crypt ( 'sathiya', gen_salt('md5') );
5 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
The postgreSQL crypt command may not work on your environment and display the following error message.
ERROR: function gen_salt("unknown") does not exist
HINT: No function matches the given name and argument types.
You may need to add explicit type casts.
To solve this problem, installl the postgresql-contrib-your-version package and execute the following command
in the postgreSQL prompt.
# \i /usr/share/postgresql/8.1/contrib/pgcrypto.sql
Get free Unix tutorials, tips and tricks straight to your email in-box.
you@address.com Subscribe
Print Friendly
RSS Feed
Tags: pg_database_size Function, pg_size_pretty function, postgreSQL Btree Index, PostgreSQL database,
postgreSQL Database Encryption, PostgreSQL DBA Commands, postgreSQL Hash Index, postgreSQL
Performance Tuning, PostgreSQL psql command
6 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
{ 3 trackbacks }
Adam,
PostgreSQL data types are divided into base types, composite types, domains, and pseudo-types.
http://developer.postgresql.org/pgdocs/postgres/extend-type-system.html
Great article. The commands to find overall table/database size were extremely useful.
For getting the second-minimum of a table, if you are not concerned with tie values, it’s much faster to use
SELECT m FROM mytable ORDER BY m LIMIT 1 OFFSET 1;
if m is indexed
Leave a Comment
7 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
Name
Website
Next post: Turbocharge Firefox Browser With Vim Editor Functionality Using Vimperator Add-on
Search
Follow us on Twitter
8 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
"Vim offers just about everything you could ever want from an editor. The
best that can happen is when an experienced user shows you the way and
accompanies you as you learn. This book does exactly this. "
Download eBook
POPULAR POSTS
Get a Grip on the Grep! - 15 Practical Grep Command Examples
Linux 101 Hacks - Download Free eBook
6 Steps to Secure Your Home Wireless Network
Backup and Restore MySQL Database Using mysqldump
Linux Crontab: 15 Awesome Cron Job Examples
Turbocharge PuTTY with 12 Powerful Add-Ons - Software for Geeks #3
Mommy, I found it! -- 15 Practical Linux Find Command Examples
Unix LS Command: 15 Practical Examples
How To Monitor Remote Windows Machine Using Nagios on Linux
15 Examples To Master Linux Command Line History
CATEGORIES
9 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
10 of 11 7/7/2010 8:17 AM
15 Advanced PostgreSQL Commands with Examples http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-command...
troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write
articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and
the blog.
Networking
Follow us on Twitter
Contact Us
Contact Me : Use this Contact Form to get in touch me for your comments, questions or suggestions
about this site. You can also simply drop me a line to say hello!.
Mobile Version: Go to m.thegeekstuff.com on your mobile to access this blog from your phone.
11 of 11 7/7/2010 8:17 AM