Home >Backend Development >Golang >How to Resolve 'pq: SSL is not enabled on the server' Error in Go's Postgres Connection?

How to Resolve 'pq: SSL is not enabled on the server' Error in Go's Postgres Connection?

Barbara Streisand
Barbara StreisandOriginal
2024-12-19 14:57:25867browse

How to Resolve

SSL Encryption Disabled Error with Postgres Database in Go

When attempting to connect to a Postgres database in Go, you may encounter the error message "pq: SSL is not enabled on the server" if you try to prepare a statement using db.Prepare().

To resolve this issue, it is necessary to establish the database connection without SSL encryption. This can be achieved by setting the sslmode parameter to disable when creating the connection.

The following code snippet demonstrates how to connect to the database without SSL encryption:

import (
    "database/sql"
)

// create a database connection without SSL encryption
db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable")
if err != nil {
    // handle error
}

// prepare the statement without SSL encryption
stmt, err := db.Prepare(selectStatement)
if err != nil {
    // handle error
}

By disabling SSL encryption during the database connection, you can prepare statements and execute queries without encountering the "SSL is not enabled on the server" error.

The above is the detailed content of How to Resolve 'pq: SSL is not enabled on the server' Error in Go's Postgres Connection?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn