15 Advanced PostgreSQL Commands with Examples


Home About Free eBook Archives Best of the Blog Contact Subscribe HowTo & FAQ
Ads by Google Postgres Database Administration Linux Commands SQL Server Database

15 Advanced PostgreSQL Commands with Examples
by SathiyaMoorthy on May 21, 2009 ShareThis Some of the open source application comes with postgreSQL database. To maintain those application, companies may not hire a fulltime postgreSQL DBA. Instead they may request the existing Oracle DBA, or Linux system administrator, or programmers to maintain the potgreSQL. In this article let discuss about the 15 practical postgresql database commands which will be useful to both DBA and expert psql users. Also, refer to our previous article about 15 Practical PostgreSQL DBA Commands.

1. How to find the largest table in the postgreSQL database?
$ /usr/local/pgsql/bin/psql test Welcome to psql 8.3.7, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit

test=# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC;

1 of 11

7/7/2010 8:17 AM

.15 Advanced PostgreSQL Commands with Examples http://www. pg_database_size -----------------63287944 (1 row) If you want it to be shown pretty. by default a page is 8kb ) pg_class – system table.com/2009/05/15-advanced-postgresql-command. pg_size_pretty ---------------55 MB (1 row) How to find size of the postgreSQL table ( not including index ) ? Use pg_relation_size instead of pg_total_relation_size as shown below. relname | relpages ---------+---------pg_proc | 50 (1 row) relname – name of the relation/table.. relpages . # SELECT pg_size_pretty(pg_database_size('geekdb')).thegeekstuff. relpages FROM pg_class ORDER BY relpages DESC limit 1. # SELECT pg_size_pretty(pg_total_relation_size('big_table')). # SELECT pg_database_size('geekdb').relation pages ( number of pages. It shows the size in bytes. 2. You may be interested in knowing only the size of the table excluding the index then use the following command. then use pg_size_pretty function which converts the size in bytes to human understandable format. How to calculate postgreSQL database size in disk ? pg_database_size is the function which gives the size of mentioned database. 2 of 11 7/7/2010 8:17 AM . which maintains the details of relations limit 1 – limits the output to display only one row. pg_size_pretty ---------------60 MB (1 row) 3. How to calculate postgreSQL table size in disk ? This is the total disk space size used by the mentioned table including index and toasted data. 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.

attname) "pg_attribute_relid_attnum_index" UNIQUE. 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 . pg_size_pretty ---------------38 MB (1 row) 4. at the end of the output you will have a section titled as indexes.15 Advanced PostgreSQL Commands with Examples http://www. attnum) 5.. How to view the indexes of an existing postgreSQL table ? Syntax: # \d table_name As shown in the example below.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.. if you have index in that table.thegeekstuff. btree (attrelid.com/2009/05/15-advanced-postgresql-command. How to specify postgreSQL index type while creating a new index on a table ? By default the indexes are created as btree. In the example below. # SELECT pg_size_pretty(pg_relation_size('big_table')). table pg_attribute has two btree indexes. btree (attrelid. By default postgres uses btree index as it good for most common situations. test=# \d pg_attribute Table "pg_catalog.

# select count(distinct col_name) from table. 6. # EXPLAIN ANALYZE query.15 Advanced PostgreSQL Commands with Examples http://www. How to generate a series of numbers and insert it into a table ? This inserts 1. # ROLLBACK -. Following example gives the total number of rows with a specific column value is not null. How to work with postgreSQL transactions ? How to start a transaction ? # BEGIN -. But shows the plan in which it got executed. 7.start the transaction. 10. How to rollback or commit a postgreSQL transaction ? All the operations performed after the BEGIN command will be committed to the postgreSQL database only you execute the commit command. # CREATE INDEX test_index ON numbers using hash (num). # COMMIT -.rollbacks the transaction.. thus does not shows the output to the user. 8. # select count(col_name) from table. Syntax: CREATE INDEX name ON table USING index_type (column).1000)). # INSERT INTO numbers (num) VALUES ( generate_series(1. Use rollback command to undo all the transactions before it is committed. How can I get the second maximum value of a column in the table ? First maximum value of a column 4 of 11 7/7/2010 8:17 AM . Following example displays the distinct number of rows for the specified column value. How to display the plan by executing the query on the server side ? This executes the query in the server side.2.commits the transaction.com/2009/05/15-advanced-postgresql-command.thegeekstuff. 11. # select count(*) from table. 9.3 to 1000 as thousand rows in the table numbers.. How to count total number of rows in a postgreSQL table ? This shows the total number of rows in the table. How to view execution plan used by the postgreSQL for a SQL query ? # EXPLAIN query.

5 of 11 7/7/2010 8:17 AM . 12.15 Advanced PostgreSQL Commands with Examples http://www. After the redirection is enabled. # \o As explained in our earlier article. Second minimum value of a column # SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table ). # SELECT crypt ( 'sathiya'. PostgreSQL database can encrypt the data using the crypt command as shown below. How to view the basic available datatypes in postgreSQL ? Below is the partial output that displays available basic datatypes and it’s size. gen_salt('md5') ). # select max(col_name) from table. you can also backup and restore postgreSQL database using pg_dump and psql. 13. This can be used to store your custom application username and password in a custom table.com/2009/05/15-advanced-postgresql-command.. Second maximum value of a column # SELECT MAX(num) from number_table where num < ( select MAX(num) from number_table ).. The output of the query will be redirected to the “output_file”. execute the \o without any argument as mentioned below.thegeekstuff. To enable the output to the stdout again. typname | typlen ----------------+-------bool | 1 bytea | -1 char | 1 name | 64 int8 | 8 int2 | 2 int2vector | -1 typname – name of the datatype typlen – length of the datatype 14. test=# SELECT typname. How can I get the second minimum value of a column in the table ? First minimum value of a column # select min(col_name) from table. Storing the password after encryption. How to redirect the output of postgreSQL query to a file? # \o output_file # SELECT * FROM pg_class. the select command will not display the output in the stdout. 15.typlen from pg_type where typtype='b'.

Table..1/contrib/pgcrypto.thegeekstuff. postgreSQL Btree Index. Column and Index Using mysqlshow 5. PostgreSQL DBA Commands. PostgreSQL psql command 6 of 11 7/7/2010 8:17 AM ... # \i /usr/share/postgresql/8. Get Quick Info On MySQL DB. 15 Practical PostgreSQL Database Administration Commands 2. postgreSQL Database Encryption. tips and tricks straight to your email in-box. PostgreSQL crypt function Solution: To solve this problem. pg_size_pretty function. postgreSQL Hash Index. installl the postgresql-contrib-your-version package and execute the following command in the postgreSQL prompt. PostgreSQL crypt function Issue: The postgreSQL crypt command may not work on your environment and display the following error message.Linux 101 Hacks Get free Unix tutorials.com Subscribe Print Friendly RSS Feed If you enjoyed this article. you@address.sql Bookmark/Share this Article Leave a Comment Download Free eBook . How To Backup and Restore PostgreSQL Database Using pg_dump and psql 3. You may need to add explicit type casts. you might also like. How to Execute PostgreSQL Commands Inside Unix Shell Scripts 4.com/2009/05/15-advanced-postgresql-command. PostgreSQL database. 1. postgreSQL Performance Tuning.15 Advanced PostgreSQL Commands with Examples http://www. 9 Steps to Install and Configure PostgreSQL from Source on Linux Tags: pg_database_size Function. ERROR: function gen_salt("unknown") does not exist HINT: No function matches the given name and argument types.

Lazarus December 15. 2009 at 7:00 pm Daddy. typtype=’b’ means that data is a basetype.typlen from pg_type where typtype=’b’.com/2009/05/15-advanced-postgresql-command. what is meant by typtype=’b’ ? what does it refer? 2 Harsh Agrawal May 24. 5 Andrew J.postgresql. 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 . I found it!.thegeekstuff.. { 3 trackbacks } 15 Advanced PostgreSQL Commands with Examples . 2009 at 6:15 am Your article looks great. b==basetype.. 15 Awesome Linux Find Command Examples (Part2) June 29..org/pgdocs/postgres/extend-type-system.DbRunas May 21.15 Advanced PostgreSQL Commands with Examples http://www. 2009 at 12:32 pm For getting the second-minimum of a table. The commands to find overall table/database size were extremely useful. 2009 at 2:36 pm Thanks man. if you are not concerned with tie values. This is very useful for me 3 Srini May 31. 2009 at 12:14 pm Great article. 2009 at 4:52 pm Adam. 2009 at 5:50 pm { 5 comments… read them below or add one } 1 adam May 23. 2009 at 12:29 pm アイビースター | PostgreSQLのちょっとしたSQLの15の方法 May 26. and pseudo-types. In 13th command – How to view the basic available datatypes in postgreSQL ? SELECT typname. domains. PostgreSQL data types are divided into base types. composite types. http://developer.html 4 Chris August 27.

com/2009/05/15-advanced-postgresql-command.. Name E-mail Website Notify me of followup comments via e-mail Previous post: Oracle LSNRCTL – Listener Shutdown and Startup Procedures Next post: Turbocharge Firefox Browser With Vim Editor Functionality Using Vimperator Add-on Search Sign up for our free email newsletter you@address.thegeekstuff.15 Advanced PostgreSQL Commands with Examples http://www..com Follow us on Twitter Become a fan at Facebook Subscribe via RSS Sign Up VIM 101 HACKS EBOOK 8 of 11 7/7/2010 8:17 AM .

The Secure Shell 5.. Fritz Mehner (Author of several Vim plugins) Download eBook POPULAR POSTS Get a Grip on the Grep! .0 Tutorials MySQL PostgreSQL Oracle 12 AMAZING LINUX BOOKS 1.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 .thegeekstuff. Essential System Administration 9 of 11 7/7/2010 8:17 AM . Sed and Awk 2. This book does exactly this. Dr. "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..15 Advanced PostgreSQL Commands with Examples http://www. " Prof. Learning the Vi and Vim Editors 3. I found it! -. SSH.Software for Geeks #3 Mommy.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 Vi / Vim Tips and Tricks UNIX Sed Tips and Tricks Linux Tutorials SSH Tips and Tricks Productivity Tips HowTo & FAQ Hardware Articles Nagios 3.15 Practical Grep Command Examples Linux 101 Hacks .com/2009/05/15-advanced-postgresql-command. Bash Cookbook 4.

Understanding the Linux Kernel 9. Linux Administration Handbook 12.15 Advanced PostgreSQL Commands with Examples http://www. Linux Cookbook 10.. how-to. 10 of 11 7/7/2010 8:17 AM . DNS and BIND 8. I will be posting instruction guides. Beginning Ubuntu Linux Read full review of these 12 books About The Geek Stuff My name is Ramesh Natarajan. Linux Server Hacks. 6.com/2009/05/15-advanced-postgresql-command. Linux Firewalls 11..thegeekstuff. Volume One 7.

All rights reserved | Terms of Service Advertise | Questions or Comments 11 of 11 7/7/2010 8:17 AM . Networking Follow us on Twitter Facebook Contact Us Contact Me : Use this Contact Form to get in touch me for your comments.. troubleshooting tips and tricks on Linux. security and web. Copyright © 2008–2010 Ramesh Natarajan..thegeekstuff. Mobile Version: Go to m. Read more about Ramesh Natarajan and the blog. You can also simply drop me a line to say hello!.thegeekstuff. database. questions or suggestions about this site. My focus is to write articles that will either teach you or help you resolve a problem.com/2009/05/15-advanced-postgresql-command.com on your mobile to access this blog from your phone.15 Advanced PostgreSQL Commands with Examples http://www. hardware.

Sign up to vote on this title
UsefulNot useful