pg_dump --dbname=postgresql://username:password@127.0.0.1:5432/mydatabase
OR
Create a .pgpass file in the home directory of the account that pg_dump will run as.
The format is:
hostname:port:database:username:password
Then, set the file's mode to 0600. Otherwise, it will be ignored.
chmod 600 ~/.pgpass
pg_dump -h 127.0.0.1 -U user db_name > file_name.bak