Forrest logo
tool overview
On this page you find all important commands for the CLI tool psql. If the command you are looking for is missing please ask our AI.

psql

psql is a command-line tool used for interacting with PostgreSQL database servers. It stands for "PostgreSQL interactive terminal" and provides a user-friendly interface for executing SQL queries and managing databases.

  1. psql allows you to connect to a PostgreSQL server by specifying the server name, port number, username, and password through command-line arguments or environment variables.
  2. Once connected, psql provides a command prompt where you can enter SQL statements, database operations, and various meta-commands.
  3. It supports both single-line and multi-line SQL queries, making it suitable for complex database interactions.
  4. psql supports tab-completion, allowing you to quickly navigate through tables, columns, and PostgreSQL keywords.
  5. It provides an interactive and intuitive environment for viewing, modifying, and managing database objects such as tables, views, indexes, and functions.
  6. With psql, you can create, drop, and alter databases, schemas, and roles, enabling comprehensive database administration.
  7. It supports transaction management, allowing you to begin, commit, and rollback transactions to maintain data integrity.
  8. psql provides various formatting options to control the output of your SQL queries, making it easier to read and interpret the results.
  9. It includes a command history feature that enables you to access previously executed commands, saving time during repetitive tasks.
  10. psql also has advanced features like support for scripting, input and output redirection, executing external commands, and working with CSV and other file formats.

Overall, psql is a powerful and versatile command-line tool that simplifies database management and facilitates efficient interaction with PostgreSQL servers.

List of commands for psql:

  • postgresql:export:csv Store result of a PostgreSQL query as a CSV file
    $ psql -d dbname -t -A -F"," -c "${query}" > ${file_name}
    try on your machine
    explain this command
  • postgresql:import:sql Import SQL dump into a PostgreSQL database
    $ psql ${database_name} < ${database_dump}
    try on your machine
    explain this command
  • psql:tldr:7978b psql: Execute commands from a file on the given database.
    $ psql ${database} -f ${file-sql}
    try on your machine
    explain this command
  • psql:tldr:83e3a psql: Connect to the database; user will be prompted for password.
    $ psql -h ${host} -p ${port} -U ${username} -W ${database}
    try on your machine
    explain this command
  • psql:tldr:ad719 psql: Connect to the database. By default, it connects to the local socket using port 5432 with the currently logged in user.
    $ psql ${database}
    try on your machine
    explain this command
  • psql:tldr:afdb1 psql: Connect to the database on given server host running on given port with given username, without a password prompt.
    $ psql -h ${host} -p ${port} -U ${username} ${database}
    try on your machine
    explain this command
  • 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
    explain this command
  • sql:warp:2944c Execute a .sql file against a PostgreSQL database
    $ psql -U ${user_name} -d ${database_name} -a -f ${file_name}
    try on your machine
    explain this command
tool overview