JIYIK CN >

Current Location:Home > Learning > DATABASE > PostgreSQL >

Connect to PostgreSQL using a password

Author:JIYIK Last Updated:2025/04/11 Views:

This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpassa file, PGPASSWORDan environment variable or a connection string.

Connecting to PostgreSQL with a password using the command line interface (CLI)

If you have PostgreSQL installed on your machine, you can try different ways to connect to the database. A simple way is to type it in the command line psql, which will ask you for adminthe user's password.

If you just type without mentioning the username in the command psql, you will be asked for the PostgreSQL admin user password as shown below.

C:\Users\Admin>psql
Password for user Admin:

If you want to log in using a different account, you need to use the flag -Uand then provide the username as shown below.

C:\Users\Admin>psql -U postgres
Password for user postgres:
psql (14.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=#

Here, the CLI asks for the user postgres's password. Logging in is a two-step process.

First, psqlyou define the username in the command, and then when the CLI asks, you enter the password. But we can connect to PostgreSQL directly using a single line of command.

To do this, we need to set environment variables PGPASSWORD.

In Windows:

C:\Users\Admin>set PGPASSWORD=root

In Linux:

export PGPASSWORD=root

If we want to connect to psql, it will PGPASSWORDget the password from , which is now available in the environment.

C:\Users\Admin>psql -U postgres
psql (14.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

You see, now it does not require a password to connect psql. Learn more about PostgreSQL environment variables from here .

pgpass.confDefine the password to log in to PostgreSQL in the file

In the PostgreSQL APPDATAfolder, there is a pgpass.conffile called . You can define your password there.

passfileAdditionally, you can define your password file using the connection parameters . The file format is as follows.

hostname:port:database:username:password

This structure is mentioned on the PostgreSQL official page here .

It knows the database the user is valid in and the password for that specific user.

Connecting to PostgreSQL using a connection string with a password

When you get localhostan online database or a remote database that is not in , you need to connect through a connection string. The connection string contains a username, password, database name, port, and host address.

The format of the connection string is as follows.

postgresql://<username>:<password>@<host><port>/<database_name>?sslmode=require

If you use PostgreSQL localhost, you don't need to use it for remote databases sslmode. This method is most useful when using a PostgreSQL database in a Python, C++, or Java project.

So, the command to connect to the database (local) using the connection string is as follows.

C:\Users\Admin>psql postgresql://postgres:root@localhost:5432/postgres
psql (14.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=#

Here is a list of methods available for client authentication. It comes from the PostgreSQL official documentation.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Terminate the PostgreSQL connection

Publish Date:2025/04/11 Views:199 Category:PostgreSQL

In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep

Single query to rename and change column type in PostgreSQL

Publish Date:2025/04/11 Views:166 Category:PostgreSQL

This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat

Joining columns using Select in PostgreSQL

Publish Date:2025/04/11 Views:176 Category:PostgreSQL

MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi

Using CASE in PostgreSQL

Publish Date:2025/04/11 Views:124 Category:PostgreSQL

This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,

Using NOT IN with subqueries in PostgreSQL

Publish Date:2025/04/11 Views:93 Category:PostgreSQL

NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr

Using variables in PostgreSQL

Publish Date:2025/04/11 Views:171 Category:PostgreSQL

This article will demonstrate how we can declare and assign values ​​to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y

Deleting a database in PostgreSQL via PSQL

Publish Date:2025/04/11 Views:166 Category:PostgreSQL

There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t

Using the database in PostgreSQL

Publish Date:2025/04/11 Views:132 Category:PostgreSQL

This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab

PostgreSQL replace string

Publish Date:2025/04/11 Views:91 Category:PostgreSQL

This article discusses how to use PostgreSQL replace() functions to replace strings. Use the PostgreSQL replace() function to replace strings PostgreSQL replace() functions have the following arguments, which are all text types: replace (st

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial