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 -
[email protected]:~$ sudo -i -u postgres [email protected]:~$ psql psql (9.3.5, server 9.3.6) Type "help" for help.
Redhat based systems like Centos / Fedora :
Connect/login as root -
[email protected]:~$ su - postgres [email protected]:~$ psql psql (9.3.6) Type "help" for help.
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
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]]
|Print all nonempty input lines to standard output as they are read. This is equivalent to setting the variable ECHO to all.|
|Switches to unaligned output mode.|
|Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.|
|Secifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.|
|Copy all SQL commands sent to the server to standard output as well. This is equivalent to setting the variable ECHO to queries.|
|Use the file filename as the source of commands instead of reading commands interactively. After the file is processed, psql terminates. This is in many ways equivalent to the meta-command \i.|
|Use separator as the field separator for unaligned output. This is equivalent to \pset fieldsep or \f.|
|Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix-domain socket.|
|Turn on HTML tabular output. This is equivalent to \pset format html or the \H command.|
|List all available databases, then exit. Other non-connection options are ignored. This is similar to the meta-command \list.|
|Write all query output into file filename, in addition to the normal output destination.|
|Do not use Readline for line editing and do not use the command history. This can be useful to turn off tab expansion when cutting and pasting.|
|Put all query output into file filename. This is equivalent to the command \o.|
|Specifies the TCP port or the local Unix-domain socket file extension on which the server is listening for connections. Defaults to the value of the PGPORT environment variable or, if not set, to the port specified at compile time, usually 5432.|
|Specifies printing options, in the style of \pset. Note that here you have to separate name and value with an equal sign instead of space. For example, to set the output format to LaTeX, you could write -P format=latex.|
|Specifies that psql should do its work quietly. By default, it prints welcome messages and various informational output. If this option is used, none of this happens. This is useful with the -c option. This is equivalent to setting the variable QUIET to on.|
|Use separator as the record separator for unaligned output.|
|Runs in single-line mode where a newline terminates an SQL command, as a semicolon does.|
|Turnoff printing of column names and result row count footers, etc.|
|Specifies options to be placed within the HTML table tag. See \pset for details.|
|Connect to the database as the user username instead of the default. (You must have permission to do so, of course.)|
|Perform a variable assignment, like the \set meta-command. Note that you must separate name and value if any, by an equal sign on the command line.|
|Print the psql version and exit.|
|Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.|
|Force psql to prompt for a password before connecting to a database.|
|Turn on the expanded table formatting mode.|
|Do not read the start-up file.|
|Set the field separator for unaligned output to a zero byte.|
|Set the record separator for unaligned output to a zero byte. This is useful for interfacing, for example, with xargs -0.|
|When psql executes a script with the -f option, adding this option wraps BEGIN/COMMIT around the script to execute it as a single transaction. This ensures that either all the commands complete successfully, or no changes are applied.|
|Show help about psql command line arguments and exit.|
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.
Clicking on pgAdmin III following screen will come :
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.
Under the Database(s) on this server section, find the desired database and execute SQL queries :