From PostgreSQL wiki

Most access to the database, including the psql program, goes through the libpq library. This library includes a feature where if you specify a file called .pgpass (or the file referenced by PGPASSFILE) you can put the password needed to connect as a user in there. This allows automating routine administration tasks through mechanisms like cron.

The format of the .pgpass file is the following:

hostname:port:database:username:password

the character '*' can match any value in any of the fields (except password)

n.b.: if the environment variable PGPASSWORD is set, then the ~/.pgpass file is not read