connecting via unix socket to a non-standard port
Posted: 18.01.2024, 17:43
(I use google translate for english, sorry )
how can I connect to a local postgresql server via a unix socket in zeoslib, but when the DBMS is on a non-standard port (for example, 5433) ?
debian 10 (astralinux 1.7 "Smolensk" ), lazarus 3.0.0, zeoslib r8073, postgresql 11, all default settings except the port number (port = 5433), peer authentication, unix domain socket file:
Through the standard program (from postgresql-client package) I connect as “psql -p 5433” and everything works, peer authentication passes.
but when I try to do the same through zeoslib:
I see an error:
error: connection to server on socket
"/var/run/postgresql/.s.PGSQL.5432"
failed: No such file or directory
Is the server running locally and accepting connections on that socket ?
Why is it trying to connect to the unix socket " .s.PGSQL.5432" if I explicitly specified port=5433 in ZConnection ?
documentation for postgresql:
error: connection to server on socket
"/var/run/postgresql/.s.PGSQL.5432"
failed: No such file or directory
Is the server running locally and accepting connections on that socket ?
Code: 7 Message: Connect to "var/run/postgresql/:5433" as user ""
It seems that the host in ZConnection is NOT the same as the host in libpq - i.e. ZConnection always tries to connect via tcp/ip and does not take into account that the host property may begin with a slash and this means using unix sockets.
but if the "host" property in zconnection is empty, it ignores the "port" property and always connects to 5432, but connects via a unix socket (I changed the postgresql port to the default 5432 and tried it - for the default connection via unix socket and peer, authentication works: no need to configure nothing at all except specifying the 'postgresql' protocol in zconnection).
how to force ZConnection to use a unix socket, but correctly pass the port number (the way "psql -p 5433" is done - it connects via a unix socket and correctly adds the port number as an extension to the file in '/var/run/postgresql/.s.PGSQL.') ?
P.S. if we use the standard TPQConnection from lazarus - everything is fine in it, there the parameters specified in the "Params" property are passed to libpq and we can specify it like this:
and everything connects correctly (only the port was specified, the host was not specified and it uses a unix socket) both through a unix socket and to port 5433.
Can someone add the same property to ZConnection (protocol='postgresql'), in which we can specify any parameters and they would be passed without changes to libpq in order to already use any functionality (properties) specified on libpq parameter key words ?
how can I connect to a local postgresql server via a unix socket in zeoslib, but when the DBMS is on a non-standard port (for example, 5433) ?
debian 10 (astralinux 1.7 "Smolensk" ), lazarus 3.0.0, zeoslib r8073, postgresql 11, all default settings except the port number (port = 5433), peer authentication, unix domain socket file:
Code: Select all
ls -a /var/run/postgresql/.s*
/var/run/postgresql/.s.PGSQL.5433 /var/run/postgresql/.s.PGSQL.5433.lock
but when I try to do the same through zeoslib:
Code: Select all
var
zconn1: TZConnection;
begin
zconn1 := TZConnection.Create(nil);
zconn1.Protocol := 'postgresql';
zconn1.Port := 5433;
zconn1.Connect;
error: connection to server on socket
"/var/run/postgresql/.s.PGSQL.5432"
failed: No such file or directory
Is the server running locally and accepting connections on that socket ?
Why is it trying to connect to the unix socket " .s.PGSQL.5432" if I explicitly specified port=5433 in ZConnection ?
documentation for postgresql:
ok, let's try to explicitly specify the path to the directory in host:host
Name of host to connect to. If a host name begins with a slash, it specifies Unix-domain communication rather than TCP/IP communication; the value is the name of the directory in which the socket file is stored. The default behavior when host is not specified, or is empty, is to connect to a Unix-domain socket in /tmp (or whatever socket directory was specified when PostgreSQL was built).
Code: Select all
var
zconn1: TZConnection;
begin
zconn1 := TZConnection.Create(nil);
zconn1.Protocol := 'postgresql';
zconn1.Port := 5433;
zconn1.Host := '/var/run/postgresql/';
zconn1.Connect;
"/var/run/postgresql/.s.PGSQL.5432"
failed: No such file or directory
Is the server running locally and accepting connections on that socket ?
Code: 7 Message: Connect to "var/run/postgresql/:5433" as user ""
It seems that the host in ZConnection is NOT the same as the host in libpq - i.e. ZConnection always tries to connect via tcp/ip and does not take into account that the host property may begin with a slash and this means using unix sockets.
but if the "host" property in zconnection is empty, it ignores the "port" property and always connects to 5432, but connects via a unix socket (I changed the postgresql port to the default 5432 and tried it - for the default connection via unix socket and peer, authentication works: no need to configure nothing at all except specifying the 'postgresql' protocol in zconnection).
how to force ZConnection to use a unix socket, but correctly pass the port number (the way "psql -p 5433" is done - it connects via a unix socket and correctly adds the port number as an extension to the file in '/var/run/postgresql/.s.PGSQL.') ?
P.S. if we use the standard TPQConnection from lazarus - everything is fine in it, there the parameters specified in the "Params" property are passed to libpq and we can specify it like this:
Code: Select all
var
PQConnection1: TPQConnection;
begin
PQConnection1 := TPQConnection.Create(nil);
PQConnection1.LoginPrompt := false;
PQConnection1.KeepConnection := false;
PQConnection1.Params.Text := 'port=5433';
PQConnection1.Connected := true;
Can someone add the same property to ZConnection (protocol='postgresql'), in which we can specify any parameters and they would be passed without changes to libpq in order to already use any functionality (properties) specified on libpq parameter key words ?