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.
{.power-number}
-
Switch to the
postgresuser.$ sudo su postgres
-
Open the PostgreSQL interactive terminal
psql:$ psql
:material-information: Hint: You can connect to
psqlas thepostgresuser in one go:$ sudo su - postgres -c psql
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
psqlterminal:$ \q
To learn more about using psql, see psql :octicons-link-external-16: documentation.
Congratulations! You have connected to PostgreSQL and learned some essential psql commands.
Manipulate data in PostgreSQL :material-arrow-right:{.md-button}