Search code examples
databasepostgresqlubuntuconnection

disconnecting pgadmin's connection via bash


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)


Solution

  • Upgrade to PostgreSQL v13 and use

    dropdb --force veganary_test