PostgreSQL commands to interact with database

Once the postgreSQL client version is installed we can switch over to the postgres account on your server by typing:
$ sudo -i -u postgres


Now we can check the postgresql version using the following command:
$ psql --version


We can now access a Postgres prompt immediately by typing:
$ psql


Now if we want to go to the pgres prompt without switching the account we can do that using the following command:
$ sudo -u postgres psql


Following are few postgresql queries with \ command:
  • \q: Quit/Exit
  • \c __database__: Connect to a database
  • \d __table__: Show table definition including triggers
  • \dt *.*: List tables from all schemas (if *.* is omitted will only show SEARCH_PATH ones)
  • \l: List databases
  • \dn: List schemas
  • \df: List functions
  • \dv: List views
  • \df+ __function : Show function SQL code.
  • \x: Pretty-format query results instead of the not-so-useful ASCII tables

Comments