Connect to PostgreSQL Database on Linux, Windows | w3resource
w3resource

Connect to PostgreSQL Database on Linux, Windows


Connect to PostgreSQL from the command line

Running the PostgreSQL interactive terminal program, called psql, which allows you to interactively enter, edit, and execute SQL commands. At the time of installing postgres to your operating system, it creates an "initial DB" and starts the postgres server domain running. Typically initdb creates a table named "postgres" owned by user "current logged in user name"

At the command line in your operating system, type the following command.

Debian based systems like Ubuntu :

Connect/login as root -

user@user-pc:~$ sudo -i -u postgres
postgres@user-pc:~$ psql
psql (9.3.5, server 9.3.6)
Type "help" for help.

Redhat based systems like Centos / Fedora :

Connect/login as root -
user@user-pc:~$ su - postgres
user@user-pc:~$ psql
psql (9.3.6)  
Type "help" for help.

Windows :

In windows, current user doesn't matter

C:\Program Files\PostgreSQL\9.4\bin>psql -U postgres
Password for user postgres:
psql (9.4.1)
Type "help" for help.

postgres=#

After accessing a PostgreSQL database, you can run SQL queries and more. Here are some common psql commands

  • To view help for psql commands, type \?.
  • To view help for SQL commands, type \h.
  • To view information about the current database connection, type \conninfo.
  • To list the database's tables and their respective owners, type \dt.
  • To list all of the tables, views, and sequences in the database, type \z.
  • To exit the psql program, type \q.

What is psql?

psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries interactively, sent them to PostgreSQL, and see the query results.

psql [option...] [dbname [username]]

Connect to PostgreSQL database using pgAdmin GUI application

You can also connect to PostgreSQL database using pgAdmin GUI application. Connect to the database at localhost:5432 using the user name postgres and the password supplied.

postgresql pgadminIII

Clicking on pgAdmin III following screen will come:

postgresql pgadminIII part2

Now, double click on PostgreSQL 9.4 under the "Servers Groups". pgAdmin will ask you for a password. You have to supply the password for the postgres user for authentication.

postgresql-pgadminIII part3

Under the Database(s) on this server section, find the desired database and execute SQL queries:

postgresql-pgadminIII Part 4

PREV : PostgreSQL Installation
NEXT : PostgreSQL Connector and APIs



Follow us on Facebook and Twitter for latest update.