UnixTech

Creativity, Business - Amplified

Improve PostgreSQL CLI

2 min read

When you work in Terminal/Browser workflow for almost 10 hours a day - 5 days a week, It becomes important kind of colors and configs you choose. For me Database has been important part of my workflow when configuring various business logic for applications.

I mainly use PostgreSQL for storing almost everything that has to resambles data, and mostly it will be automated through psycopg2 in python scripts but time to time I do dwell in CMD option that PostgreSQL provides through psql.
Given the configurability of psqlrc and flexibility that is allowed by PostgreSQL server, it's almost surprising that how little people take advantage of these available features. Aliases and setting up proper History files can be useful features that comes in handy.

PostgreSQL stores psqrc at various levels in system.

  • System wide psqlrc
    • Will Affect all users
    • Can be located using following
  pg_config --sysconfdir
  /usr/local/etc/postgresql

Note: This is for FreeBSD operating system, location will vary as per your own OS.

  • Per User psqlrc
 touch ~/.psqlrc

You can also have multiple psqlrc one per major version of PostgreSQL on your system.

if you have more then one version of PostgreSQL installed on your system, then name it accordingly. Ex. For version 9.4 - psqlrc-9.4 or psqlrc-9.4.3. This way It will enable you to have multiple configuration files for each user and per version as well.

Now, Decide on which specific configuration file you want to configure - System wide or User specific and start customizing your psqlrc.

Actual configuration file


-- This is comment.
\set PROMPT1 '%n@%/%R%x%# '
\set PROMPT2 '[more] %R > '
\pset null '[null]'
\set COMP_KEYWORD_CASE upper
\timing
\set PAGER less
\set HISTSIZE 2000
\encoding unicode
\x auto
\pset border 2
\set VERBOSITY verbose
\set version 'SELECT version();'

-- MACRO can be defined like this.
\set extensions 'select * from pg_available_extensions;'
\echo 'Welcome to Dev1 PostgreSQL \n'

Final output

Wrapping up

These are about the main settings that you would want to configure here, Apart from these settings only Aliases as per your convinience should be configured inside your PostgreSQL configuration file so repetation can be avoided. Putting it in version controlled dotfiles git repository and you will be able to sync same setting across all your DB server regardless So, Give custom configs a try!