PostgreSQL – Connect to database from command line using password

Use the below command to connect to the PostgreSQL database from command line using psql utility. The -W option will allow the utility to prompt for the password.

psql -h hostname -d databasename -p port -u username -W

The below command can be used to connect to the PostgreSQL database from command line while passing password non-interactively.

PGPASSWORD=yourpassword psql -h hostname -d databasename -p port -u username

Here is the syntax to run a stored procedure from command line non-interactively. You can copy the above into a shell script and schedule it via crontab.

PGPASSWORD=yourpassword psql -h hostname -d databasename -p port -u username <<EOF
CALL your_stored_proc();
EOF
See also  PostgreSQL - Move Sequence to a different Schema