Access Redshift using psql without Password Prompt – Use psql System Variables

  • Post author:
  • Post last modified:January 27, 2023
  • Post category:Redshift
  • Reading time:5 mins read

Users can access Amazon Redshift database using a PostgreSQL- compatible psql client. Users can always connect to the Redshift database via leader node. In this article, we will see how to access redshift using psql without password prompt. You can use psql system variables to store required credentials.

Access Redshift using psql without Password Prompt

Access Redshift using psql without Password Prompt

Password less access is useful when you are trying to execute Redshift queries from scripts such as shell or perl scripts.

Option 1. Export Amazon Redshift Database Environmental Variables

In order to access Redshift database with no password prompt, you need to set up some environmental variables.

Read:

PostgreSQL or psql supports below environmental variables:

Environmental VariableDescription
PGHOSTThe host name of the Redshift Database. This is usually your Redshift cluster information. This is required if you are connecting Redshift database from edge node or EC2 host systems
PGDATABASEThe name of the Redshift database to which you want to connect to.
PGPORTThe port number that the Redshift Database instance is running on. Default port number is 5439.
PGUSERRedshift Database user name to connect. Typically, in the project life cycle, Developer, Tester and prod users will be different.
PGPASSWORDRedshift Database user password.

You have to export the above mentioned system variables in Unix/Linux bash shell.

Follow below steps to connect to Amazon Redshift using system or environmental variables.

https://gist.github.com/398df203eeebcbaec97892bc352f2adf

Option 2. Use a .pgpass file to store the password

The one more option is to use .pgpass file to store your Redshift password. This is one of the secured and best way to hide your Redshift cluster credentials. You can also control the access to the password file. The file .pgpass in a user’s home directory or the file referenced by PGPASSFILE (set password file location to PGPASSFILE environmental variable) can contain passwords to be used if the connection requires a password.

File should contains information in following line format. You can even add comments to the file by preceding it with # symbol.

hostname:port:database:username:password

For instance, below is the location of file to be used when connection requires password.

https://gist.github.com/14ae504798aa1997e69fb82b6999f92f

Whenever connection requires the password, Redshift database get it from .pgpass file stored in particular user $HOME directory.

Feel free to add any other easy methods that you follow in your organization.

Related Articles