How to use the command psql (with examples)

How to use the command psql (with examples)

The psql command is the PostgreSQL command-line client, which allows users to interact with a PostgreSQL database directly from the command line. It provides a convenient way to execute SQL queries and PostgreSQL commands, connect to databases, and perform various database-related tasks.

Use case 1: Connect to the database

Code:

psql database

Motivation: This use case is useful when you want to connect to a local database using the default settings. It saves you from specifying the host, port, and user every time you want to connect.

Explanation:

  • psql: The command itself.
  • database: The name of the database to connect to.

Example output:

psql (13.1)
Type "help" for help.

database=#

Use case 2: Connect to the database on a specific server

Code:

psql -h host -p port -U username database

Motivation: You might need to connect to a database located on a remote server. In this case, you can specify the host, port, and username to establish a connection.

Explanation:

  • -h host: Specifies the host (server) where the database is located.
  • -p port: Specifies the port number for the database server.
  • -U username: Specifies the username to connect to the database.
  • database: The name of the database to connect to.

Example output:

psql (13.1)
Type "help" for help.

database=#

Use case 3: Connect to the database and prompt for password

Code:

psql -h host -p port -U username -W database

Motivation: This use case is helpful when connecting to a remote database that requires authentication. It prompts for the password after specifying the host, port, and username.

Explanation:

  • -W: Prompts for the password to connect to the database.

Example output:

psql (13.1)
Password for user username:
database=#

Use case 4: Execute a single SQL query or PostgreSQL command

Code:

psql -c 'query' database

Motivation: When working with shell scripts that involve database interactions, you can use this use case to execute a single SQL query or a PostgreSQL command without entering the interactive psql shell.

Explanation:

  • -c 'query': Specifies a single SQL query or PostgreSQL command to execute.

Example output:

 column1 | column2 
---------+---------
 data1   | data2
(1 row)

Use case 5: Execute commands from a file

Code:

psql database -f file.sql

Motivation: This use case allows you to execute a series of SQL commands stored in a file. It is useful when you have a large number of queries or commands to execute.

Explanation:

  • -f file.sql: Specifies the file containing SQL commands to execute.

Example output:

INSERT 0 1
UPDATE 1
DELETE 1

Conclusion:

The psql command provides a powerful and flexible way to interact with PostgreSQL databases from the command line. With its various use cases, you can connect to databases, execute SQL queries, and perform database-related tasks efficiently. Whether you need to connect locally or remotely, execute single queries or commands, or execute commands from a file, psql has got you covered.

Related Posts

How to generate completions for different shells using the command 'pueue completions' (with examples)

How to generate completions for different shells using the command 'pueue completions' (with examples)

The ‘pueue completions’ command is used to generate shell completion files for various shells, including Bash, Elvish, Fish, PowerShell, and Zsh.

Read More
Using the `open` Command (with examples)

Using the `open` Command (with examples)

The open command in macOS allows users to open files, directories, and applications.

Read More
How to use the command 'xcv' (with examples)

How to use the command 'xcv' (with examples)

The ‘xcv’ command is a helpful tool for cutting, copying, and pasting files in the command-line.

Read More