Psql Windows Exit Status Codes – Explanation

  • Post author:
  • Post last modified:April 5, 2022
  • Post category:Redshift
  • Reading time:5 mins read

The psql is a command line interface to connect to any postgreSQL relational database systems such as Redshift, Greenplum, etc. You can connect to your Redshift or any other PostgreSQL system using psql and execute queries without needing any particular tool. Your psql execution command may get fail and you need a status code to provide a proper error message to client. In this article, we will check what are psql windows exit status codes with explanation.

Psql Windows Exit Status Codes

When you execute queries interactively using psql prompt on windows command line interface or Putty, the command will return set of exit status codes that you can transform into meaningful error message in case your psql execution failed.

The psql command will return following exit status code on windows machine.

  • 0: Returns zero if finished normally.
  • 1: Returns 1 if a fatal error of its own occurs (e.g. out of memory, file not found)
  • 2: Returns 2 if the connection to the server went bad and the session was not interactive, and
  • 3: Returns 3 if an error occurred in an SQL script and the variable ON_ERROR_STOP was set.

Redshift ON_ERROR_STOP

You can also use an ON_ERROR_STOP variable to control your SQL file execution. You have to set this variable inside your SQL file or on psql command prompt.

You will get more information on setting Redshift ON_ERROR_STOP nmy other post: Redshift SET ON_ERROR_STOP using psql and Examples

Psql Windows Exit Status Codes Examples

To use psql on windows, first, you should download it from the PostgreSQL official website and install it on your local machine. Note that, it requires administrator privileges to install the tools.

You don’t need to install PostgreSQL, you can only install PostgreSQL tools to use psql command line interface.

For example, to check the Windows exit status codes, execute commands and check the exit code by displaying content of ‘echo %errorlevel%‘:

commands and check the exit code by displaying content of ‘echo %errorlevel%‘:

C:\Users\viths>psql -h Redshift-example-cluster-us-west-2.redshift.com -U user1 -d dev -p 5439 -c "select * from test";
Password for user user1 :
ERROR:  relation "test" does not exist

C:\Users\viths>echo %errorlevel%
1

Related Articles

Hope this helps 🙂