disconnecting pgadmin's connection via bash - database

after registering the user in the postgres database(in ubuntu) we can execute some basic command like dropdb, createdb directly from the terminal to alter the database.
I basically was creating a shell script to renew the database. So, I thought doing this would suffice:
dropdb veganary_test && createdb veganary_test
as always, I was wrong. Since I had multiple connections to the database, db wouldn't drop. I also tried this:
psql <database_name> -c "SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '<database_name>'
AND pid <> pg_backend_pid();"
and, yet it wasn't successful on disrupting pgadmin's connection. How can I disconnect every user connected to my <database_name> database from the terminal?(bash)

Upgrade to PostgreSQL v13 and use
dropdb --force veganary_test

Related

How to stop postgresql PGPOOL replication

I'm trying postgres database backup and restore. The access to postgres is through pgpool.
To achieve the back up I am following the official postgres pg_dumpall documentation.
Commands taken from postgres website: https://www.postgresql.org/docs/14/app-pg-dumpall.html
$ pg_dumpall > db.out
To reload database(s) from this file, you can use:
$ psql -f db.out postgres
The backup works fine.
However, when attempting to restore, I'm getting the following error because of replication feature enabled by pgpool.
psql:tmp/backup/postgresDump/pg_data.out:15: ERROR: database "xyz" is being accessed by other users
DETAIL: There is 1 other session using the database.
Here are the following ideas I tried by browsing other SO questions.
I tried to update active='f' in pg_catalog.pg_replication_slots view. It failed with the error below
DETAIL: Views that do not select from a single table or view are not automatically updatable.
HINT: To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
List the process ids for the replication slot and used pg_terminate_backend along with the pid, followed by restore.
Command to terminate the replication slot
psql -U postgres -h pgpool.default.cluster.local -c "SELECT pg_terminate_backend(3402)"
pg_terminate_backend
----------------------
f
(1 row)
As per the second answer in this link, Postgresql - unable to drop database because of some auto connections to DB
I executed the terminate_backend command multiple times until it returned 0 results. Although this step was successful, restore failed with error saying
psql:tmp/postgresDump/pg_data.out:14: ERROR: database "xyz" is being accessed by other users
DETAIL: There is 1 other session using the database.
Looks like as soon as I drop a replication slot, the pgpool recreates the replication slot and establishes a connection for it.
Tried by dropping the replication_slot following the official documentation.
https://www.postgresql.org/docs/9.5/functions-admin.html
psql -U postgres -h pgpool.default.local -c "select pg_drop_replication_slot('repmgr_slot_1001');"
ERROR: replication slot "repmgr_slot_1001" is active for PID 3402
Any information on how to execute restore functionality through psql is highly appreciated.

In springboot, can I connect with two database so that if one database is giving exception then I can connect to the other database dynamically

Requirement in detail:
So I have two databases (both are in sync) and somehow one goes down and Spring Boot application starts giving exceptions. In this case I want the application to connect to the second database.
Please help me with this.
Thanks in advance.
As you have a DataGuard implementation in Oracle with a Primary database and another one in Standby mode, Oracle Transparent Application Failover is the way to go.
Transparent Application Failover (TAF) is a feature of the Java
Database Connectivity (JDBC) Oracle Call Interface (OCI) driver. It
enables the application to automatically reconnect to a database, if
the database instance to which the connection is made fails. In this
case, the active transactions roll back.
Database Setup
I am assuming your implementation of DG uses Oracle Restart.
Datatase: TESTDB
Service in TAF: TESTDB_HA
Primary site
srvctl add service -d testdb -s testdb_ha -l PRIMARY -y AUTOMATIC -e select -m BASIC -z 200 -w 1
srvctl start service -d testdb -s testdbha
Standby site
srvctl add service -d testdb -s testdb_ha-l PRIMARY -y AUTOMATIC -e select -m BASIC -z 200 -w 1
srvctl modify service -d testdb -s testdb_ha -failovermethod basic
Your JDBC connection
jdbc:oracle:thin:#(description=(address=(host=primaryserver)(protocol=tcp)(port=yourdbport))(address=(host=standbyserver)(protocol=tcp)(port=yourport))(failover=yes)(connect_data=(service_name=testdb_ha)(failover_mode=(type=select)(method=basic))))
In this setup, in case a failover from Primary to Standby, the connection will keep working once the failover is completed without manual intervention.
I am using this configuration currently in applications store in Kubernetes, using Spring Boot and/or Hibernate, and in normal Jboss Java applications. I have personally tested failover scenarios totally transparent for the applications. Obviously, if you have a transaction or query running in the moment the failover is being performed, you will get an error. But you don't need to manually change any jdbc settings in case of switch from primary site to standby site.

connection to nodejs SQL Server on mac

Probably a simple thing, but new to me. I could not find a post that helps with this, so I expect that I am doing something silly without knowing.
I have a SQL Server database running in Docker on mac & can easily log in to the 'sa' account.
I made another id for an application to connect to the same database.
CREATE LOGIN tisApp WITH PASSWORD='pa$Sw0rd'
SELECT * FROM sys.sql_logins WHERE name = 'tisApp'
However, when I try to connect to it, like this...
mssql -u tisApp -p pa$Sw0rd
I receive an error...
My-iMac:~ my$ mssql -u tisApp -p pa$Sw0rd
Connecting to localhost...
Error: Login failed for user 'tisApp'.
My-iMac:~ my$
The MS documentation states that an id created this way should be able to connect but...I cannot.
When I check the logs, they show:
Login failed for user 'tisApp'. Reason: Password did not match that for the login provided. [CLIENT: 172.17.0.1]
In-case this helps anyone, this was pretty simple answer after a small experiment. SQL Server was not accepting special characters through mssql for some reason. So, I updated the commands as follows and everything works perfectly now.
Of course, this is not a good password, but it works ok for now.
SQL Server:
CREATE LOGIN tisApp WITH PASSWORD='pasSw0rd'
SELECT * FROM sys.sql_logins WHERE name = 'tisApp'
Command line:
mssql -u tisApp -p pa$Sw0rd
Now, I get...
My-iMac:TIS my$ mssql -u tisApp -p pasSw0rd
Connecting to localhost...done
sql-cli version 0.6.2
Enter ".help" for usage hints.
mssql>

barman PostgreSQL: FAILED

I am in the process of setting up a database backup system using Barman.
My database is a postgreSQL db.
When I run
barman check main-db
I get the following error:
PostgreSQL: FAILED
directories: OK
retention policy settings: OK
backup maximum age: FAILED (interval provided: 1 day, latest backup age: No available backups)
compression settings: OK
minimum redundancy requirements: OK (have 0 backups, expected at least 0)
ssh: OK (PostgreSQL server)
not in recovery: OK
The code I'm using in my barman.conf:
ssh_command = ssh postgres#10.0.0.XX
conninfo = host=10.0.0.XX user=YYYYYYY dbname=ZZZZZZZZ
retention_policy_mode = auto
retention_policy = RECOVERY WINDOW OF 7 days
wal_retention_policy = main
Any help would be greatly appreciated
The barman check output contains two errors, the first one is critical:
PostgreSQL: FAILED
It means that your barman user cannot connect with PostgreSQL using the credentials you provided in the conninfo parameter.
You can try it yourself by becoming the barman user and executing psql passing the content of conninfo as the only argument (it requires PostgreSQL clients installed on the server):
psql 'host=10.0.0.XX user=YYYYYYY dbname=ZZZZZZZZ'
It must connect to the target PostgreSQL servers without asking for any password.
The backup maximum age error is normal because you don't have any backup. However, it is not a blocking one, so it will not prevent you from taking your first backup.
This issue has now been resolved.
I solved this issue by ensuring that conninfo= had all the correct information. (Including password= field)
I think you deleted your barman user from PostgreSQL or you recreated that PostgreSQL cluster using postgresql-12-setup initdb command. Either you recover from old backup or you can do follow below steps.
postgres#pg$ createuser -s -P barman
postgres#pg$ psql
GRANT EXECUTE ON FUNCTION pg_start_backup(text, boolean, boolean) to barman;
GRANT EXECUTE ON FUNCTION pg_stop_backup() to barman;
GRANT EXECUTE ON FUNCTION pg_stop_backup(boolean, boolean) to barman;
GRANT EXECUTE ON FUNCTION pg_switch_wal() to barman;
GRANT EXECUTE ON FUNCTION pg_create_restore_point(text) to barman;
GRANT pg_read_all_settings TO barman;
GRANT pg_read_all_stats TO barman;
For streaming backup, you need to create streaming connection.
postgres#pg$ createuser -P --replication streaming_barman
Next update your pg_hba.conf file with your granted ip address.

Form info to database on another server

I have two websites; website1.com and website2.com. On website1 I have database with customer information and a neat CMS. Now I would like to make a registration form on website2 and insert that information in the database on website1.
Is this possible? Can I simply use mysql INSERT INTO~? Or do I need to use something else?
You can connect remotely to another server:
mysql -h host_name -D db_name -ppassword -c "insert into ..."
You need to allow MySQL remote connections if your websites are on different machines. Securing Remote MySQL
In both cases (remote or local), the method is the same: open a connection to website1's database and use INSERT INTO.

Resources