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;
Related
On PostgreSQL 9.6.6, running on Amazon RDS
SELECT * FROM pg_stat_activity WHERE datname = 'my_db'
yields nothing.
However, trying to drop the database
DROP DATABASE my_db;
fails with:
ERROR: database "my_db" is being accessed by other users DETAIL:
There are 3 other sessions using the database.
I've tried to lock down the number of conns to the db with these two three:
REVOKE CONNECT ON DATABASE my_db FROM public;
ALTER DATABASE my_db CONNECTION LIMIT 0;
ALTER DATABASE my_db WITH ALLOW_CONNECTIONS false;
I do have read replication set up (however — this shouldn't block as this is reading off the WAL?) and occasional snapshotting (not during the DROP stmt is being run though).
What are these three lingering conns and how do I get rid of them?
I have two POSTGRES databases stored in different servers.
The "Firstdb" is version 9.2 and it is stored in a LAN server, port 5432.
The "Seconddb" is version 10 and it is stored as localhost to my PC, port 5432.
I have access to both of them through pgAdmin4 version 2.0.
I would like to run query between those two databases to compare data.
Any ideas about how this can be done?
Thank you all for your time.
For running federated queries I use most of the time postgres_fdw, which creates a foreign table in the source database. Quite handy but has its caveats when dealing with joins.
An example:
CREATE SERVER my_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'target.host.com', port '5432', dbname 'targetdb');
CREATE USER MAPPING FOR postgres SERVER my_server OPTIONS (user 'postgres');
CREATE FOREIGN TABLE my_foreign_table (
id INT,
desc TEXT
)
SERVER my_server OPTIONS (schema_name 'public', table_name 'target_table');
EDIT based on the comments:
Keep in mind that the source database, as any other application, needs access to the target database and it has to be described at the pg_hba.conf:
host yourdb youruser 0.0.0.0 md5
Another approach is using dblink, which does not create a foreign table but enables you to fire queries directly to the target database and retrieve the result sets just as if it was local.
How to select a role when connecting to a PostgreSQL database with JDBC?
Use the user connection property, as described in the documentation.
As autonomus transactions are not avaliable in postgres we are using dblink as a workaroud but there is a scenerio which is as
linked from Does Postgres support nested or autonomous transactions?
select * from dblink_connect('TEST1','host=localhost port=5432 dbname=tyguy user=postgres password=postgres') as t1(text);
Then when we use the method
SELECT dblink_get_connections();
then only one connection is visible that is absolutely correct named as 'TEST1'
but when i open a new pg admin window or a new session then the same is not avaliable as
SELECT dblink_get_connections();
returns empty. No connection of name 'TEST1'
I am confused is DBlink is at database level ? or session level .
if it is at session level then how many dblink named connections can be opened in a database.
I'm trying to rename a database in my Redshift cluster.
You cannot rename the database when you're connected to it so I've created a temporary database, reconnected with SQL Workbench to the temporary db and issued:
ALTER DATABASE olddb RENAME to newdb;
I get an error stating ERROR: database "olddb" is being accessed by other users [SQL State=55006]
I've checked who is connected and there appear to be some connections from user rdsdb to the database. I assume this is a service account that AWS Redshift use to perform maintenance tasks etc.
How can I rename the database when this superuser is connected?
Many thanks.
You cannot alter the name of (or delete!) the database that is created during the initial cluster creation. I don't believe this is mentioned in the docs but I've confirmed it with them.
We can change the database name which is already created.
Detailed steps on how to do
Connect to the old database and create a new database if you do not have another one already.
create database databasebasename
In this example, I will call the databasename as 'newdb'.
Connect to newdb using connecting string as, jdbc:redshift://.us-east-1.redshift.amazonaws.com:8192/newdb, with the same password and username of your superuser (or the other eligible users as mentioned above).
Now you can alter the database name. Substitute 'database_name_new' with the desired databasename.
alter database old-db-name rename to database_name_new;
If there are any active sessions, you'll have to kill them. To find the pid of active sessions:
select * from STV_SESSIONS where user_name='rdsdb';
Then to kill a session:
SELECT
pg_terminate_backend(<pid>)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
procpid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = '<old-db-name>';
Once complete, you can connect back to that new database using the new name in the connection string as
jdbc:redshift://<cluser-id>.us-east-1.redshift.amazonaws.com:8192/database_name_new
You can delete the temporary 'newdb'.
drop database databasebasename
That's possible now -- I just renamed the database that was created during the initial cluster creation.
We had a similar situation.
Step 1: Connect to the database which is not the one you are trying to rename. Check the same by executing SELECT CURRENT_DATABASE();.
Step 2: Execute the query below -
SELECT
ss.*, 'select pg_terminate_backend('||process||');'
FROM
stv_sessions ss
ORDER BY
db_name;
The output of the query will have a column at the end with the select statements. Execute those to kill the sessions.
Step 3(Optional): If you are not the owner of the database try to modify the ownership of the database -
ALTER DATABASE <database to be renamed>
OWNER TO <user which is going to do the rename>;
Step 4: Rename the database