15 Advanced PostgreSQL Commands with Examples
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.
1. How to find the largest table in the postgreSQL database?
If you want only the first biggest table in the postgres database then append the above query with limit as:
- relname
- relpages – relation pages ( number of pages, by default a page is 8kb )
- pg_class
- limit 1
2. How to calculate postgreSQL database size in disk ?
pg_database_size is the function which gives the size of mentioned database. It shows the size in bytes.
If you want it to be shown pretty, then use pg_size_pretty function which converts the size in bytes to human understandable format.
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. You may be interested in knowing only the size of the table excluding the index then use the following command.
How to find size of the postgreSQL table ( not including index ) ?
Use pg_relation_size instead of pg_total_relation_size as shown below.
4. How to view the indexes of an existing postgreSQL table ?
Syntax: # \d table_name
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.
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.
6. How to work with postgreSQL transactions ?
How to start a transaction ?
# BEGIN -- start the transaction.
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. 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 ?
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.
9. How to generate a series of numbers and insert it into a table ?
This inserts 1,2,3 to 1000 as thousand rows in the table numbers.
10. How to count total number of rows in a postgreSQL table ?
This shows the total number of rows in the table.
Following example gives the total number of rows with a specific column value is not null.
Following example displays the distinct number of rows for the specified column value.
11. How can I get the second maximum value of a column in the table ?
First maximum value of a column
Second maximum value of a column
# 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 ?
First minimum value of a column
Second minimum value of a column
# SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table );
13. How to view the basic available datatypes in postgreSQL ?
Below is the partial output that displays available basic datatypes and it’s size.
- typname – name of the datatype
- typlen – length of the datatype
14. How to redirect the output of postgreSQL query to a file?
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.
As explained in our earlier article, you can also backup and restore postgreSQL database using pg_dump and psql.
15. Storing the password after encryption.
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.
PostgreSQL crypt function Issue:
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.
PostgreSQL crypt function Solution:
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