2.0 KiB
Connect Using psql
psql
is an interactive command-line utility that enables you to interact with a PostgreSQL database server. Using psql
, you can perform various SQL operations on your database.
Installation
Before you can start using psql
, you need to ensure that it is installed on your computer. It gets installed automatically alongside the PostgreSQL server, but if you need to install it separately, follow the steps from the "Installation and Setup" section of this guide.
Accessing psql
To connect to a PostgreSQL database using psql
, open your terminal (on Linux or macOS) or Command Prompt (on Windows), and run the following command:
psql -h localhost -U myuser mydb
Replace "localhost" with the address of the PostgreSQL server, "myuser" with your PostgreSQL username, and "mydb" with the name of the database you want to connect to.
You'll be prompted to enter your password. Enter it, and you should see the psql
prompt:
mydb=>
Basic psql
commands
Here are some basic commands to help you interact with your PostgreSQL database using psql
:
-
To execute an SQL query, simply type it at the prompt followed by a semicolon (
;
), and hit enter. For example:mydb=> SELECT * FROM mytable;
-
To quit
psql
, type\q
and hit enter:mydb=> \q
-
To list all databases in your PostgreSQL server, use the
\l
command:mydb=> \l
-
To switch to another database, use the
\c
command followed by the database name:mydb=> \c anotherdb
-
To list all tables in the current database, use the
\dt
command:mydb=> \dt
-
To get information about a specific table, use the
\d
command followed by the table name:mydb=> \d mytable
Conclusion
psql
is a powerful, command-line PostgreSQL client that lets you interact with your databases easily. With its simple, easy-to-use interface and useful commands, psql
has proven to be an indispensable tool for database administrators and developers alike.