Introduction
PostgreSQL is an open source relational database management system.
Psql is an interactive terminal program for working with PostgreSQL. Use psql to edit, automate, and execute queries in PostgreSQL.
pgAdmin is a web interface for managing PostgreSQL databases. It provides a visual, user-friendly environment with a host of practical solutions that make managing databases easy.
In this tutorial, you will learn how to connect to PostgreSQL from the command line using psql or pgAdmin.
Prerequisites
- This guide assumes that you have already installed PostgreSQL and created a database you want to access.
- Access to a command-line/terminal window
- Sudo or root privileges
- pgAdmin 4 installed
How to Connect to PostgreSQL Using psql
Installing PostgreSQL creates a default database and user account, both called ‘postgres.’
To log into the ‘postgres’ user account type the following command in the terminal:
sudo –i –u postgres
This example shows the command in a Debian-based distribution, Ubuntu.
For the same result on a Red Hat–based system, (e.g., Centos and Fedora) use any of the following commands:
su postgres
or
su -i postgres
These commands open a bash shell and give the user ‘postgres’ root privileges within that shell.
Note: The same command applies if you have already created a different user and a corresponding database with the same name. If a user called ‘test1’, that role will attempt to connect to a database called ‘test1’ by default.
To begin using psql, enter the command:
psql
The following screen confirms you are now able to edit and execute queries in PostgreSQL.
PostgreSQL can support and maintain a large number of databases and users simultaneously. Once you log in, it is easy to confirm the current connection and user information.
Simply enter the command:
/conninfo
The output helps to determine which user and database you are currently interacting with.
How to Access psql Directly Using sudo
It is possible to connect with PostgreSQL directly and bypass the intermediary bash shell.
If you are sure that all the components of your databases and users are correctly configured, you can log into psql directly:
sudo –i –u postgres psql
The -u
(user) option causes sudo
to run the specified command as a user other than root. As with the previous method, you can now work with databases by executing queries.
How to Access PostgreSQL With pgAdmin
The pgAdmin is a graphical tool for managing PostgreSQL databases. After installing and configuring the latest browser version of pgAdmin 4, you need to create an initial pgAdmin user account.
The basic setup requires an email and a password to authenticate access to the web interface.
python /usr/lib/python2.7/site-packages/pgadmin4-web/setup.py
Once the email and password are defined, access the pgAdmin4 interface by using: http://localhost/pgadmin4 or http://ip-adress/pgadmin4
To authenticate, use the email address and password created previously. Once the user interface loads, add a PostgreSQL server by navigating to Servers > Create > Server.
The General and Connection tabs allow you to enter values for your server name and database user credentials.
The Hostname/address is the location of the machine where the PostgreSQL server is running. A connection with your user account is established once you save the information entered. The interface presents an overview of the databases that your user account has access to.
To enter and execute queries, click Tools > Query Tool or press ALT+Shift+Q within the current database.
Conclusion
This article provided two (2) simple solutions on how to connect to a PostgreSQL database.
If you are looking for a terminal-based solution, psql is an excellent choice for speed and effectiveness.
The GUI based pgAdmin provides a secure, user-friendly way to log in, administer, and shape databases to fit your requirements. A graphical interface can be indispensable when working on a host of databases simultaneously.
Alternatively, you can also connect to PostgreSQL with SQL Workbench.
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/226155.html