Connect to your PostgreSQL database using psql command

9 de agosto, 2016 @ 12:07 — 0 comentarios

psql is a program used on a Unix shell to connect and manage your PostgreSQL database.

Before you use the psql command you need to have access to a remote shell through SSH.

Connecting to PostgreSQL using psql

First you need to know your connection details
Host: postgresql.guebs.net
Username: user_name
Password: *********
Database: database_name

Those will be given to you, when you create a new PostgreSQL database.

Once you have the details, you can write the connection command line, substituting fields as needed:

psql -h postgresql.guebs.net -U user_name-ddatabase_name

Press enter.  The shell will ask for a password:

Password for useruser_name:

Press enter. If the password is correct, we will see something like this:

psql (9.4.8)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

database_name=>

Hereafter, you can execute the SQL queries or  postgresql commands, to manage your database.

Commands inside psql

Source: Kartones Postgres cheatsheet

psql commands summary
Task Psql command
Close conection \q
Connect to a database \c __base_datos__
List databases \l
Show table definition including triggers \d __table__
Listar Schemas \dn
List functions \df
List views \dv
Show function SQL code \df+ __function
Pretty-format \x