JIYIK CN >

Current Location:Home > Learning > DATABASE > PostgreSQL >

Connecting to PostgreSQL in SSL mode

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

When we try to connect to the database, Postgres uses SSL to verify the security of the connection. It is disabled by default in HTTP, but in HTTPS, we need the SSL mode of the connection to perform any operation in the Postgres database.

If the connection is not private, there can be multiple attack parameters. Anyone can easily use a sniffing tool through the database request-response.

What are the SSL modes in PostgreSQL?

Postgres provides different types of SSL modes. First, let's look at the generic connection string for Postgres.

const connectionString = "postgres://<database_username>:<database_userpassword>@<hostaddress>:<port_no>/<database_name>"

Now, we can add parameters sslmodeas shown below.

const connectionString = "postgres://<database_username>:<database_userpassword>@<hostaddress>:<port_no>/<database_name>?sslmode=<ssl_mode>"

This is a list of SSL modes provided by Postgres.

sslmode Eavesdropping protection Man-in-the-middle protection describe
disable No No It does not care about security. No data is encrypted.
allow Maybe No It will not care about security and encrypt the connection.
prefer Maybe No It does not force the use of encryption; if the server supports the overhead of encryption, then it will encrypt.
require Yes No By encrypting the data, it faces some encryption overhead, while the network can ensure that the user wants to connect to the right server.
verify-ca Yes Depends on CA policy Encrypt your data, minimize encryption overhead and always connect to trusted servers.
verify-full Yes Yes The data will be encrypted, the user accepts the overhead, the network and servers are trusted, and connections are only made to the specific servers that are asked.

You can also set this flag in an environment variable.

PGSSLMODE=verify-full PGSSLROOTCERT=ca.pem

Here, ca.pemis the key. You need to collect it from a CA; CA stands for Certificate Authority.

This is the description of SSL mode from the official Postgres documentation.

Using NULL-SHA or NULL-MD5 ciphers, authentication can be done without any encryption overhead. A man-in-the-middle, on the other hand, might read and pass communications between the client and the server. In addition, as compared to the overhead of authentication, encryption has a low overhead. NULL ciphers are not recommended for these reasons.

Additionally, you can check out the official documentation here . They show how to self-sign the certificate when using SSL mode.

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

Connect to PostgreSQL using a password

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

This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm

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

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial