Forrest logo
back to the psql tool

psql:tldr:f813c

psql: Execute a single SQL query or PostgreSQL command on the given database (useful in shell scripts).
$ psql -c '${query}' ${database}
try on your machine

The command "psql -c '${query}' ${database}" is a command-line command used to execute a SQL query in PostgreSQL.

Here is a breakdown of the command:

  • "psql" is the command to enter the PostgreSQL command-line interface.
  • "-c" is an option that allows you to specify the SQL command directly in the command-line.
  • "${query}" is a placeholder for the SQL query. You need to replace this placeholder with the actual SQL query you want to execute.
  • "${database}" is another placeholder that needs to be replaced with the name of the PostgreSQL database you want to connect to.

To use the command, you need to replace "${query}" with your actual SQL query and "${database}" with the name of your target database.

For example, if you want to execute the SQL query "SELECT * FROM employees;" in a database called "sampledb," the command would look like this:

psql -c 'SELECT * FROM employees;' sampledb

This would open the PostgreSQL command-line interface and execute the given SQL query in the specified database.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the psql tool