Skip to content

For help, click the link below to get free database assistance or contact our experts for personalized support.

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.

  1. Switch to the postgres user.

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

    $ psql
    

    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 documentation.

Congratulations! You have connected to PostgreSQL and learned some essential psql commands.

Next steps

Manipulate data in PostgreSQL