Want to execute a PostgreSQL command from the command line? You can! The
-c flag takes a string argument that will be executed on your database of choice.
I’ve been using it as part of a script that creates a remote database backup, downloads the backup, drops and creates a local database, dumps the database backup into the local database, and then runs a select statement on the dataset. That final command looks like this (query has been simplified):
$ psql -d tilex_prod_backup -c "select count(*) from posts"; count ------- 2311 (1 row)