Connect with database Postgresql causes segmentation fault

I have a trouble with application when i try to connect (PQconnectdb) with database Postgresql. When i don’t invoke any function after connect with database everything is OK, i can communicate with database. If, for example i invoke function ‘MainApp.Init()’ after connect, application hang (segmentation fault).
What’s wrong.
With debug gdb, gdb return error :slight_smile: Thread 1 “CameraModule” received signal SIGSEGV, Segmentation fault.
0x0000007fb6e92918 in strcmp () from /lib/aarch64-linux-gnu/

example code:

ConnectionPtr pConnection = PQconnectdb( szconn.c_str() ); //connect with database
MainApp.Init() //segmentation fault when invoke this function

Hi @Przemyslaw_Jakobczak, welcome to the Go Forum.

I am a bit confused. PQconnectdb is a function from the C library libpq. The error also stems from a C library, libc, and the example code is C code.

How is your problem connected to Go?

Hi, I use Postgressql from c library. Used functions posgressql compiled in C mixed with Go.
Problem is only why application hangs after connect, when i invoke some function in Go after connect.
When i dont’ call any functions after connect everything is ok.

Curious, is there any particular reason why you do not use a Go Postgres package (such as pq or pgx) and avoid the hassle of interfacing with C?

Your current problem looks like a problem with calling C from Go. Can you share the Go code that calls the C function? Or better, a minimized version?

Because of compatibility in code with other applications i must use interface with Postgressql in C.
Weird, when i call some functions in Go application hang, only after connect with database.Why when i don’t call any function in Go application ok works? I tried use mutexes in Go while i try to connect, and error also.

//code in Go calling C
func InitConnections(uri string) {

//here code in C
void InitCPanelInterface( const char* connString, int size )
using namespace ltm;
ConnectionPool* pConn = ConnectionPool::instance();
InitializationFunc func( &CPanelQueries::init );
pConn->init( connString, size, func );
void ConnectionPool :: init( const std::string& szconn, unsigned int size,
InitializationFunc func )
using namespace std;

LOGINFO("Initialize connection with string '%s'\n", szconn.c_str() );

for( unsigned int i = 0; i < size; i++ )

    ConnectionPtr pConnection = PQconnectdb( szconn.c_str() );  //her connect, app hangs
    LOGINFO("if( PQstatus(...\n");
    if( PQstatus( pConnection ) != CONNECTION_OK )
        pConnection = nullptr;
        LOGERROR( "Init connection [%d] failed\n", i );
        throw runtime_error( "Connection failure" );
    push( pConnection );

So the C part works as long as the Go code does not call any C functions after a database connection is made?

You mention mutexes, so I assume your Go code runs multiple goroutines. The libpq docs say that libpq may or may not be thread-safe, depending on the compile options used. If you call PQisthreadsafe(), does it return a 1?

I also wonder about the SEGV in strcmp(), which seems to indicate that a string is not properly null-terminated. (But OTHO, c_str() should ensure that, correct?) Is uri a valid connection string, and does it survive all the transformations (C.Cstring(), c_str()) it goes through?

Strange, i have added parameter ‘sslmode=disable’ in function ‘PQconnectdb’ and now is working Ok!
PQconnectdb(“user=‘cam’ password=‘cam’ host=‘’ dbname=‘cpanel’ port=‘5432’ sslmode=disable”).
Thanks for help.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.