I'm a little all over the place with this question so please read to the end. This is a program that connects to the database but really wasn't as I just found out.
I have a program in C that talks to nginx, locally, to serve web pages via FastCGI and this works well. I use spawn-fcgi for the FastCGI interface.
I want to attach a database, postgreSQL, and talk to that via unix sockets. Using a short, standalone test program, I'm able to connect to postgres, do a query and return correct results.
When I use the same code as above in my program for the web site, however, my program exits with
> upstream prematurely closed connection while reading response header
> from upstream, client: 127.0.0.1, server: localhost upstream:
> "fastcgi://unix:/var/run/fcgi-sock.fcgi:", host: "localhost"
The program exits when I try to do the connect. Leave that call to PQconnectdb() out and the program does not crash. I do not include the query.
One thought I had is there might be a permissions issue since nginx runs as www:www while postgresql runs as user pgsql:wheel but I don't know what to do with that and if that's the whole issue. At least the socket in /tmp says the owner is pgsql:wheel.
I have not made any changes to postgresql.conf
So I'm hoping someone recognizes the problem and help me out.
**EDIT:** I changed my running server code to call PQconnectdb directly.
Including that call causes the program to crash and nginx debug still shows upstream closing prematurely.
I question whether I still have a missing setting in the postgresql config but, from what I read, this should work out of the box.
I have a program in C that talks to nginx, locally, to serve web pages via FastCGI and this works well. I use spawn-fcgi for the FastCGI interface.
I want to attach a database, postgreSQL, and talk to that via unix sockets. Using a short, standalone test program, I'm able to connect to postgres, do a query and return correct results.
Code:
PGconn* connect_db(void) {
PGconn *conn = PQconnectdb("user=pgsql dbname=mydb host=/tmp password=mypass
port=5432");
if(PQstatus(conn) != CONNECTION_OK) {
return 0;
} else return conn;
}
int
main(void){
PGconn *db = NULL;
db = connect_db();
if(PQstatus(db) != CONNECTION_OK) {
printf("%s", "Connection failed");
PQfinish(db);
return 0;
}
printf("OK");
PGresult *result = PQexec(db,"select a bunch of stuff here;");
printf("%s",PQgetvalue(result,0,0));
}
When I use the same code as above in my program for the web site, however, my program exits with
signal 11
, segfault, and brings spawn-fcgi down with it causing a 502 HTTP response. The nginx error.log says:> upstream prematurely closed connection while reading response header
> from upstream, client: 127.0.0.1, server: localhost upstream:
> "fastcgi://unix:/var/run/fcgi-sock.fcgi:", host: "localhost"
The program exits when I try to do the connect. Leave that call to PQconnectdb() out and the program does not crash. I do not include the query.
One thought I had is there might be a permissions issue since nginx runs as www:www while postgresql runs as user pgsql:wheel but I don't know what to do with that and if that's the whole issue. At least the socket in /tmp says the owner is pgsql:wheel.
I have not made any changes to postgresql.conf
So I'm hoping someone recognizes the problem and help me out.
**EDIT:** I changed my running server code to call PQconnectdb directly.
Code:
PGconn *db = NULL;
db = PQconnectdb("user=pgsql dbname=mydb port=5432");
Including that call causes the program to crash and nginx debug still shows upstream closing prematurely.
I question whether I still have a missing setting in the postgresql config but, from what I read, this should work out of the box.