Connect to the PostgreSQL server¶
With PostgreSQL server up and running, let’s connect to it.
By default, the postgres
user and the postgres
database are created in PostgreSQL upon its installation and initialization. This allows you to connect to the database as the postgres
user.
-
Switch to the
postgres
user.$ sudo su postgres
-
Open the PostgreSQL interactive terminal
psql
:$ psql
Hint: You can connect to
psql
as thepostgres
user in one go:$ sudo su - postgres -c psql
Basic psql
commands¶
While connected to PostgreSQL, let’s practice some basic psql
commands to interact with the database:
-
List databases:
$ \l
-
Display tables in the current database:
$ \dt
-
Display columns in a table
$ \d <table_name>
-
Switch databases
$ \c <database_name>
-
Display users and roles
$ \du
-
Exit the
psql
terminal:$ \q
To learn more about using psql
, see psql
documentation.
Congratulations! You have connected to PostgreSQL and learned some essential psql
commands.