Require application_name for incoming connections - postgresql

I am passing the application_name to my postgres server in the uri like
postgres://username:password#host:port/database?application_name=name
My question is how I can force every incoming connection request to supply this parameter? For logging reasons I don't want to allow connections without this name.
Is that possible?

Related

How to get username of another server's admin, with given ServerResponse object?

I'm making a simple server search function where players enter their username and either host a server or join a specific server from a server list. I can successfully list all servers on a local area network by getting their IP addresses with ServerResponse object. What I want is to show host's username, instead of IP number (Or any other values owned by server client when needed, without actually joining the server). Anyone has any idea how can I achieve this? Thanks in advance.

"Socket.RemoteHost" in some networks returns IP instead of ComputerName - Delphi

I'm using "OnClientConnect" of TServerSocket to identify when a client connects to chat, and save the ComputerName of the client in a listbox to manage sending messages between clients.
The code is like this:
procedure TfrmAnaForm.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
ComputerName: string;
begin
frmChatMain.lstChatUsers.Items.Add(Socket.RemoteHost);
end;
The problem is that Normally "Socket.RemoteHost" returns "ComputerName" of the client, but in some networks, the code "Socket.RemoteHost" returns IP of client instead of "ComputerName" of client.
The RemoteHost property retrieves the client's IP address from the socket, and then performs a reverse DNS lookup of the client's hostname for that IP. If that lookup fails, RemoteHost returns a blank string, not the IP address. The only way RemoteHost can return an IP address is if that is what the DNS system actually reported.
You should not be using the RemoteHost to uniquely identify clients, because 1) it is not guaranteed to even give you a value, and 2) it is not guaranteed to be unique, such as if multiple remote clients are connecting from the same computer/network. At the very least, you must use RemoteIP+RemotePort instead of RemoteHost to identify individual connections. Though, you really should be using the TCustomWinSocket object itself to identify unique connections. Even better, require clients to login to your server with a unique ID, which you can then store inside the TCustomWinSocket.Data property so it follows the connection it belongs to.

Are there any other parameters that can be sent in the Postgresql connection other than ApplicationName, that act like ApplicationName?

Switching from DB2 to Postgresql.
I'm looking for some connection parameters to pass some strings into, that can actually be retrieved from within a Trigger Function.
DB2 has connection parameters:
ApplicationName,
ClientUser,
ClientAccountingInformation
Postgresql has connection parameters:
ApplicationName (max 64 characters)
I tried setting Transaction Level session variable using select set_config('<param_name>', '<param_value>', true) in a connection interceptor. This seems to work most of the time, but occasionally the parameter doesn't get passed; making it unreliable.
So I'm back to trying to pass Connection parameters.
Any suggestions would be greatly appreciated.

Go database/sql - Issue Commands on Reconnect

I have a small application written in Go that connects to a PostgreSQL database on another server, utilizing database/sql and lib/pq. When I start the application, it goes through and establishes that all the database tables and indexes exist. As part of this process, it issues a SET search_path TO preferredschema,public command. Then, for the remainder of the database access, I do not have to specify the schema.
From what I've determined from debugging it, when database/sql reconnects (no network is perfect), the application begins failing because the search path isn't set. Is there a way to specify commands that should be executed when it reconnects? I've searched for an event that might be able to be leveraged, but have come up empty so far.
Thanks!
From the fine manual:
Connection String Parameters
[...]
In addition to the parameters listed above, any run-time parameter that can be set at backend start time can be set in the connection string. For more information, see http://www.postgresql.org/docs/current/static/runtime-config.html.
Then if we go over to the PostgreSQL documentation, you'll see various ways of setting connection parameters such as config files, SET commands, command line switches, ...
While the desired behavior isn't exactly spelled out, it is suggested that you can put anything you'd SET right into the connection string:
connStr := "dbname=... user=... search_path=preferredschema,public"
// -----------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and since that's all there is for configuring the connection, it should be used for every connection (including reconnects).
The Connection String Parameters section of the pq documentation also tells you how to quote and escape things if whatever preferredschema really is needs it or if you have to grab a value at runtime and add it to the connection string.

How to perform an "I can reach my database" healthcheck?

I have a classic spray+slick http server which is my database access layer, and I'd like to be able to have an healthcheck route to ensure my server is still able to reach my DB.
I could do it by doing a generic sql query, but I was wondering if there was a better way to just check the connection is alive and usable without actually adding load on the database (or at least the minimum possible load).
So pretty much :
val db = Database.forConfig("app.mydb")
[...]
db.???? // Do the check here
Why do you want to avoid executing a query against the database?
I think the best health check is to actually use the database as your application would (actually connecting and running a query). With that in mind, you can perform a SELECT 1 against your DB, and verify that it responds accordingly.