Set Applicationname when using Postgres ODBC driver - postgresql

How can I set the application name when connection to Postgres with the psqLODBC driver?
You get the application name from pg_stat_activity.application_name.
I tried different Parameters in the connection string.
APP=...; application_name=...; APP_NAME=... in all different variations.
Nothings works and pg_stat_activity.application_name always returns an empty string.

Once you have established a connection in your application, you can run a SQL statement to change the application_name dynamically.
set application_name = 'doev';
You run that just like any other statement (that does not return a result) with your programming language. But remember if you have turned off autocommit in your connection, you need to commit that SET statement. If you are running in autocommit mode, that is not necessary.

Related

How to change user-agent/application name when connect to PostgreSQL? [duplicate]

I'm using tomcat connection pool org.apache.tomcat.jdbc.pool.DataSource. The connections appear in my database pg_stat_activity with empty application_name.
How could I set that application name in my java app, so I know where each connection comes from (as there will be multiple applications accessing the same db)?
You could specify the application name in the connection string.
Documentation here.
Example:
jdbc:postgresql://localhost:5435/DBNAME?ApplicationName=MyApp
Take care: the param names are case sensitive.
Use set command:
set application_name to my_application;
You can add this to the JDBC URL that you use in your connection pool definition:
jdbc:postgresql://localhost/postgres?ApplicationName=my_app
If you want to change it dynamically e.g. to reflect different modules inside your application, the you can use the SET command as shown by klin.
If you want to set programmatically, the PostgreSQL drivers also have a method you can call:
PGPoolingDataSource ds = new PGPoolingDataSource();
ds.setApplicationName(applicationName);
For those using normal connection string and not jdbc:
postgresql://other#localhost/otherdb?application_name=myapp
If you're using Python library psycopg2, here's how you can do
import psycopg2
psycopg2.connect(host=host_ip, database=db_name, user=user, password=db_pwd, application_name="Python Local Test Script")

SQL Developer 18.3: set alternative schema for individual connections automatically

I'm using several connections in SQL developer to connect to different Oracle databases. For some connections I have to change the schema to that of another user. This can be done is several ways
By using: alter session set current_schema = <otheruser>;
The drawback is that I have to enter this for every connection I want to open and with a different <otheruser> for each connection.
Using the global connection startup script in Preferences > Database > Filename for connection startup script. The drawback of this method is that SQL Developer uses the same global startup script and runs it for every connection I open. Probably trying to set a non existing schema in most -but one- connections.
Is there a way to automatically set the default schema on connecting to a database for individual connections?
Connection Schema
conn_1 Leave current schema unchanged for this connection
conn_2 Change current schema to <schema_A> for this connection
conn_3 Leave current schema unchanged for this connection
conn_4 Change current schema to <schema_B> for this connection
conn_5 Change current schema to <schema_C> for this connection
A solution will be very helpful.
No, that is not a feature. We assume when you define the connection, you are using the schema that you want to work with.
The tool is VERY connection driven - using alter session set current schema will work with queries you run in a SQL Worksheet, but won't have any effect for the rest of the tool, say browsing your tables in the Connection navigation tree.
Now, if you have PROXY connect privs, you could set up your connection to actually connect to your 'default' schema via proxy.
I show how here

Where do Postgres DBLink queries run?

All,
I'm running a query on the target server that retrieves data from a source server. My query is using the digest function. Digest is on both servers. It is embedded in a UDF that is also present on both servers. BTW, the "select" portion of the query runs perfectly on the source server.
I would think that when you submit a remote query it will execute on the remote box. I am receiving a "1 function digest(text, unknown) does not exist ..." error. Also, since all the functions are in the public schema on both servers, I don't see how Postgres is failing is find the function.
Any help appreciated.
TIA,
Mike
Queries are executed on server specified in connection string. If no host was given, then on localhost. They are executed using role from connection string. That also includes search_path of that role.
Unrelated to your question, but I would try 2 things while debugging that issue you described:
Connect to remote server using the same role as dblink connection string and executing query
schema qualify that function in dblink: public.digest(..)

How to set connection timeout value for pgAdmin?

I am using pgAdmin to connect remotely to my database as phpPgAdmin is a bit limited in its features. Only problem is if I leave the SQL window open without running a query for a few minutes, then it says I need to reconnect.
Is this a setting I need to change in my database to keep remote connections alive for longer or is it a pgAdmin setting?
It is client setting.
You need specify connect_timeout in your config file.
29.1. Database Connection Control Functions
29.14. The Connection Service File

Postgres turn on log_statement programmatically

I want to turn on logging of all SQL statements that modify the database. I could get that on my own machine by setting the log_statement flag in the configuration file, but it needs to be enabled on the user's machine. How do you enable it from program code? (I'm using Python with psycopg2 if it matters.)
Turning on logging of SQL statements that modify the database can be achieved by:
ALTER SYSTEM SET log_statement TO 'mod';
-- Make it effective by triggering configuration reload (no server restart required).
SELECT pg_reload_conf();
-- To make sure the modification is not limited to the current session scope
-- it is better to log out from postgresql and log back in.
-- Check value of log_statement configuration, expected: mod
SELECT * FROM pg_settings WHERE name = 'log_statement';
This requires superuser access rights.
Check hereafter links to documentation for more details:
ALTER SYSTEM
pg_reload_conf()
The "it needs to be enabled on the user's machine" phrase is confusing, indeed... I assume you mean "from the user (client) side".
In Postgresql some server run-time parameters can be changed from a connection, but only from a superuser - and only for those settings that do not require a server restart.
I'm not sure if that includes the many log options. You might try with something like:
SELECT set_config('log_XXX', 'off', false);
where log_XXX is to be replaced by the respective logging setting, and 'false' by the value you want to set.
If that does not work, I guess you are out of luck.