1

I tried to copy a database within the same server using postgresql server

I tried the below query

CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

And got the below error

ERROR:  source database "originaldb" is being accessed by 1 other user

So, I executed the below command

SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid();

Now none of us are able to login/connect back to the database.

When I provide the below command

psql -h 192.xx.xx.x -p 9763 -d originaldb -U postgres

It prompts for a password and upon keying password, it doesn't return any response

May I understand why does this happen? How can I connect to the db back? How do I restart/make the system to let us login back?

Can someone help us with this?

1
  • 1
    Best guess is killed backends you should not have. I'm guessing you will need to restart server. Commented Jul 16, 2021 at 19:08

1 Answer 1

1

It sounds like something is holding an access exclusive lock on a shared catalog, such as pg_database. If that is the case, no one will be able to log in until that lock gets released. I wouldn't think the session-killing code you ran would cause such a situation, though. Maybe it was just a coincidence.

If you can't find an active session, you can try using system tools to figure out what is going on, like ps -efl|fgrep postgre. Or you can just restart the whole database instance, using whatever method you would usually use to do that, like pg_ctl restart -D <data_directory> or sudo service postgresql restart or some GUI method if you are on an OS that does that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.