Anything you can do from the command line (CLI) can be done from a script. Sometimes its as simple as just creating a simple text file that contains the information you would have put on the command line.
Databases also typically have SQL (Structured Query Language) scripts that use commands unique to them (e.g. select data from table where something is something).
It is common to write shell scripts that call sql scripts.
I've not worked with Postgres but did find the following thread that may give you some help. (It also leads you to the site that is designed for Postgres users that may offer you more help):
http://archives.postgresql.org/pgsql...6/msg00010.php