Get session number from namespace USERENV in postgresql - postgresql

While migrating oracle to postgresql I came up with the query:
Oracle:
select SYS_CONTEXT('USERENV','SID') from dual;
What will be the equivalent in PostgreSQL?

The only thing I can think of is the backend PID
select pg_backend_pid();

Related

Cloud SQL (GCP) postgres - how to show database number of connections configured

I would like to know the number of connections configured for a postgres database (not the max_connections setting for the instance). By default it is set to -1 (unlimited) but someone has changed that configuration for a particular database. How can I determine what the current setting is for a specific database on a GCP Cloud SQL Postgres instance?
This command is used to change the connection limit for a postgres db:
alter database yourDBName CONNECTION LIMIT nbr;
What is "nbr" for a particular database?
This query will do it:
select
datname as DbName,
datconnlimit as DbConnectionLimit
from
pg_database
order by
datname asc;

Inserting differntial data into postgresql table from db2 table trough trigger

I am looking the differential data load from db2 tables to PostgreSQL. Is there any free tool? or how I can write the trigger to connect two databases?
will db2_fdw in PostgreSQL will help me to do this?
You can't write to nicknames in triggers in Db2.
You may use SQL Replication to a federated data source (PostgreSQL in you case) depending on your Db2 version.
11.5 Federation for PG
11.1 Federation for PG

Incorrect syntax near 'PROCEDURE' postgresql

My database is hosted in Amazon and I am using pgAdmin 4 to connect to it.
I copy-pasted snippet from https://www.postgresql.org/docs/11/sql-createprocedure.html
CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
$$;
The issue is that I get 'incorrect syntax near 'PROCEDURE' ' error
What is done wrong? Not sure how I check version of postgresql itself
With Postgres 10, you need to use a function:
CREATE function insert_data(a integer, b integer)
returns void
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a), (b);
$$;
According to PostgreSQL documentation, syntax is supported in versions 11 and 12.
PostgreSQL: Documentation: 11: CREATE PROCEDURE
Documentation → PostgreSQL 11
Supported Versions: Current (11)
Development Versions: 12 / devel
Check PostgreSQL version on your server, run this query from PgAdmin:
SELECT version();

How to drop database which has some sessions open

I am trying to drop a database using the following command and I get an error
DROP DATABASE IF EXISTS mydb;
There are 5 other sessions using the database.
Is there any sql statement/sequence of queries which which drops database killing all the open sessions? I can only use commnand line.
I tried SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = 'mydb'; and also tried restarting postgres service which ideally should have worked but it did not.
OK the followoing command worked for me great
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'mydb' AND pid <> pg_backend_pid();

DB2 via IBM Client Access ODBC Driver - list all tables and columns

im connected with an DB2 database via IBM Client Access ODBC Driver.
How can i list all tables and columns?
Like in SQL-Server information_schema.columns or in Oracle all_tab_columns?
Regards,
Dennis
List all tables and columns
select * from qsys2.syscolumns