Skip to content

Latest commit

 

History

History
73 lines (46 loc) · 1.62 KB

File metadata and controls

73 lines (46 loc) · 1.62 KB

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. {.power-number}

  1. Switch to the postgres user.

    $ sudo su postgres
  2. Open the PostgreSQL interactive terminal psql:

    $ psql

    :material-information: Hint: You can connect to psql as the postgres 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:

  1. List databases:

    $ \l
  2. Display tables in the current database:

    $ \dt
  3. Display columns in a table

    $ \d <table_name>
  4. Switch databases

    $ \c <database_name>
  5. Display users and roles

    $ \du
  6. Exit the psql terminal:

    $ \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.

Next steps

Manipulate data in PostgreSQL :material-arrow-right:{.md-button}