PostgreSQL - Rename database - sql

I need to rename the database but when I do in
PGAdmin : ALTER DATABASE "databaseName" RENAME TO "databaseNameOld" it told me that it cannot.
How can I do it?
(Version 8.3 on WindowsXP)
Update
The first error message : Cannot because I was connect to it. So I selected an other database and did the queries.
I get a second error message telling me that it has come user connect. I see in the PGAdmin screen that it has many PID but they are inactive... I do not see how to kill them.

Try not quoting the database name:
ALTER DATABASE people RENAME TO customers;
Also ensure that there are no other clients connected to the database at the time. Lastly, try posting the error message it returns so we can get a bit more information.

For future reference, you should be able to:
-- disconnect from the database to be renamed
\c postgres
-- force disconnect all other clients from the database to be renamed
SELECT pg_terminate_backend( pid )
FROM pg_stat_activity
WHERE pid <> pg_backend_pid( )
AND datname = 'name of database';
-- rename the database (it should now have zero clients)
ALTER DATABASE "name of database" RENAME TO "new name of database";
Note that table pg_stat_activity column pid was named as procpid in versions prior to 9.2. So if your PostgreSQL version is lower than 9.2, use procpid instead of pid.

I just ran into this and below is what worked:
1) pgAdmin is one of the sessions. Use psql instead.
2) Stop the pgBouncer and/or scheduler services on Windows as these also create sessions

Unexist told me in comment to restart the database and it works! Restarting the database kill all existing connection and then I connect to an other database and was able to rename it with my initial query.
Thx all.

Instead of deploying a nuke (restarting the server) you should try to close those connections that bother you either by finding where are they from and shutting down the client processes or by using the pg_cancel_backend() function.

When connected via pgadmin, the default database will be postgres.
ALTER DATABASE postgres RENAME TO pgnew;
This will not work.
You need to right click on server in pgadmin and set Maintenance DB to some other DB and save. Then retry and it should work if no other connections exists.

For anyone running into this issue using DBeaver and getting an error message like this:
ERROR: database "my_stubborn_db" is being accessed by other users
Detail: There is 1 other session using the database.
Disconnect your current connection, and reconnect to the same server with a connection that doesn't target the database you are renaming.
Changing the active database is not enough.

ALTER DATABASE old_database RENAME TO new_database;
old_database means already existing database.
new_database means need to modify this name.
Example: ALTER DATABASE profile RENAME TO address;

Related

DATABASECHANGELOGLOCK and DATABASECHANGELOG tables

In our application there are four SQL Server databases:
db1
db2
db3
db4
While connecting to SQL Server using liquibase we use db2 in connection string and then perform deployment.
As db2 is used in connection string DATABASECHANGELOGLOCK and DATABASECHANGELOG are created in db2 database.
As a part of deployment, every SQL script has database name at the start like
Script1.sql
USE db1
GO
—update/delete statements
GO
Script2.sql
USE db2
GO
—update/delete statements
GO
Script3.sql
USE db3
GO
—update/delete statements
GO
Script4.sql
USE db4
GO
—update/delete statements
GO
If the last script in the deployment is db2 (as it is specified in the connection string and the DATABASECHANGELOGLOCK and DATABASECHANGELOG) are created in db2 database. Then the deployment succeeds
But if the last script is of some other database (other than db2) I get an error:
liquibase.exception.LockException: liquibase.exception.LockException: Did not update change log lock correctly.
Seems it goes to search the DATABASECHANGELOGLOCK and DATABASECHANGELOG in the database in which last script is deployed.
How I can handle this?
Found in: Liquibase lock - reasons?
Sometimes if the update application is abruptly stopped, then the lock remains stuck.
Then running
UPDATE DATABASECHANGELOGLOCK SET LOCKED=0, LOCKGRANTED=null, LOCKEDBY=null where ID=1;
against the database helps.
You may also need to replace LOCKED=0 with LOCKED=FALSE.
Or you can simply drop the DATABASECHANGELOGLOCK table, it will be recreated.

ORA-00054 error when trying to delete table

When I try this command on oracle sql-developer :
delete from my_table;
I get the following error :
ORA-00054: resource busy and acquire with NOWAIT specified
I haven't figured out how to solve this problem.
Someone or yourself(unintentionally) has a lock on the table.
Query the table V$LOCKED_OBJECTS to find the name of the user (you would want OS_USER_NAME) locking it.
You can ask the user to unlock the table. They will have to either commit/rollback their transaction. If they are unable to do so and permit you to kill their session, you can use the below:
alter system kill session 'sid, serial#'
to kill their session which has a lock on your table where 'sid' & 'serial#' is something you have to find by querying v$session,v$sqlarea tables.
This can also be done in SQL developer, you can find out the session (if your user has access to meta tables) from the menu Tools --> Monitor Sessions
Right click on the correct session record (very important) and select mark for kill. You should be able to delete the records once the session is killed.
Check with database admin. They can check if anything is going on with the table. May be someone is performing something on the table in different session. Admin can kill the session. Then, you will be able to delete/truncate the table.

How to unlock table in sybase?

im trying to change some types on my table in sybase, but when i change it, the output is this one.
User 'DBA' has the row in 'table' locked
How to unlock this?
I would:
determine the connection_id using sa_locks (documentation here)
issue the drop connection *connection_id* statement to the connection that is causing the lock to the table.
Use it with care!

Upsizing Access to SQL Server

I use Access 2010 and SQL Server 2005. I am new to the process of "upsizing" which I understand is a legacy term. When I make changes to published tables, I like to localize them back into Access, alter them with the Access interface, and then "re-upsize" them to SQL Server. When I "re-uspize" an altered table Access warns me:
"A table named xxxx already exists. Do you want to overwrite it?"
I choose yes. Then Access reports an error
"Server Error 3726: Could not drop object 'xxxx' because it is
referenced by a FOREIGN KEY constraint."
I understand the importance of foreign key constraints. I have encountered this same trouble using MySQL. In MySQL I would simply set Foreign_Key_Checks = 0; before the import, then set Foreign_Key_Checks = 1; when finished.
Unfortunately in SQL Server, a table cannot be dropped while it's keys are only disabled, they must be deleted. I don't want to delete and recreate foreign keys every time I alter a table. Do I need to start altering my tables in the SQL Server environment? Is there a way to easily "Re-upsize" a table and ignore foreign Key constraints?
If you need to use Access for a front end, instead of keeping an Access DB locally and dealing with the issues of moving back and forth. Try to use Access and connect directly to a version of the sql database you can develop against directly through access. You will probably want to look into using a linked datasource in Access to SQL.
Connecting SQL Server to an Access Database

Oracle table entries lost.

Yesterday I installed Oracle 11g & Oracle Data Access. With the installed sql*plus console I used /nolog and the connect / as SYSDBA, then I created a few users and grand them privilegs.
I created a table and insert some values. After closing and reopening the console, the table exists but the entries are lost.
I also tried to login in as an user and created a table... but after restart the entries are lost again.
Could it be that the enviroment variable is wrong?
Like John said, use the commit; statement to make your changes permanent.