Sign up for Hasura Newsletter

PostgreSQL Clients

Postgres can be connected from a programming language, a CLI tool or a GUI. Postgres follows the Client Server architecture where the server listens to requests from the client and returns a response.

In the end, the clients are just wrappers over SQL commands that get executed on the server and return a response.

Some of the popular client applications for PostgreSQL include

  • pg_dump - Extracting database into a file
  • pg_restore - Restoring a database from a file
  • create_db - Create a new PostgreSQL database
  • create_user - Create a new PostgreSQL user account
  • psql - Interactive terminal

The full list of client applications is available here.

We will make use of psql for the rest of the tutorial. It being an interactive terminal, we can execute commands, SQL statements and control the database as a whole.

psql

Based on the keyword/value and connection URI that we generated in the previous step, we will connect to the database and execute a few commands to try out a few thing.

psql connect to database

psql -h 192.168.0.118 -d postgres -U postgres -p 5432

You will be prompted to enter the password.

Replace the host IP with your localhost IP. Once the above command is successful, you will be able to start executing SQL statements.

If this is successful, you should see something like:

psql (13.3)
Type "help" for help.
postgres=#

Now you can start executing commands and SQL statements. Let's do a few examples.

psql create database

create database hasura;

Here, we are creating a new database called hasura.

psql list databases

Next up, let us list down all the databases that are available in this instance.

\list

The output of the above command should yield something like this:

List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
hasura | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)

We can see the newly created database hasura along with the existing postgres database that is available from our installation setup.

psql list tables

\dt

Since we don't have any tables created, this would return Did not find any relations.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2021 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin