How to login and authenticate to Postgresql after a fresh install?
There are two methods you can use. Both require creating a user and a database.
By default psql connects to the database with the same name as the user. So there is a convention to make that the «user’s database». And there is no reason to break that convention if your user only needs one database. We’ll be using mydatabase as the example database name.
Using createuser and createdb, we can be explicit about the database name,
CREATE ROLE myuser LOGIN PASSWORD 'mypass'; CREATE DATABASE mydatabase WITH OWNER = myuser;
$ psql -h localhost -d mydatabase -U myuser -p
$ grep "port log in" to the operating system as postgres. You're supposed to have root to get to authenticate as postgres.
It's normally not password protected and delegates to the host operating system. This is a good thing. This normally means in order to log in as postgres which is the PostgreSQL equivalent of SQL Server's SA, you have to have write-access to the underlying data files. And, that means that you could normally wreck havoc anyway.
By keeping this disabled, you remove the risk of a brute force attack through a named super-user. Concealing and obscuring the name of the superuser has advantages.
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">Share
I understand that the problems do not occur if using a DB super user (-s) instead of a non-super user. Next question for me would be, what can be done do get this running even the DB user is not a super user.
– Hartmut Pfarr
Mar 16, 2013 at 16:09
1
In my case, after creating the user with: $ sudo -u postgres createuser -s $USER, I needed to configure a Password with: ALTER USER myuser WITH PASSWORD 'mypass'; ( after logged in postgres with: sudo -u postgres psql )
– JeanCarlos Chavarria
Apr 29, 2018 at 14:13
How should I understand the command sudo -u postgres psql postgres? I understand sudo means root privilege, -u postgres means log in as user postgres, psql is the command to connect to postgresql, but what's the last postgres doing ?
– yuqli
Dec 29, 2018 at 15:22
Is this really making it the user's database? logging in as the user, I did show config_file and show data_directory and apparently the data_directory still belongs to user postgres..
– stucash
Dec 13, 2022 at 12:44
I'm kind of lost why under linux postgres would put data directory under /root by default; database is of arbitrary usage, all sorts of data could go into a database and that's almost surely user action, not root action.
– stucash
Dec 13, 2022 at 12:51
Add a comment|
57
by default you would need to use the postgres user:
How to Connect to a PostgreSQL Database From Command Line in Linux
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.
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:
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:
These commands open a bash shell and give the user ‘postgres’ root privileges within that shell.
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.
Note: Check out our in-depth article on different ways to create a Postgres user.
To begin using psql, enter the command:
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.
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:
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.
Once the email and password are defined, access the pgAdmin4 interface by using:
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.
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.