cli for postgres in linux

If  your PostgreSQL server is not started, start it with the following command.

sudo service postgresql start

To getting started with postgreSQL, open your Terminal, and type:

username$ sudo su - postgres

Now you are logged into your vm with the postgres user. Then connect to template1 database using the postgresql’s psql client:

postgres ~$  psql template1
postgres ~$  template1=#

Note the # symbol, declaring that you are logged to the database as a Super User.

Once you are done and want to disconnect from the database using \q, and quit from postgres user using ctrl+d.

Using Psql client

Let’s see some useful commands of the psql tool. First of all: you can get some help just typing \?in your psql command line. To exit from the appearing screen, just press the q button.

To load and execute SQL statemens from file, you can use this syntax:

\i my/file/path.sql

Writing long lines of SQL code in the command line can be really hard to debug. Use the \i to run from file or use:


To edit the input buffer with an editor. If you usually don’t use editor from command line use nano – it’s the simplest. To check what relations your database contains, use


If you want more information about a particular relation, use

\d myfancytable

Have fun!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s