How do I change the maintenance database for Postgres? - database

I'm running PostgreSQL version 9.0 on OSX version 10.6.6. Somehow one of my development databases has become the maintenance db, not postgres (this db also exists). I can't find any documentation on how to change/set the maintenance db back to postgres.
I can't drop my development database because of this issue...

You can change maintenance db from pgAdmin but you have to be disconnected from the database engine to be able to do that.
First disconnect:
Then in the database server properties:
Choose the desired maintenance database:

You're not entirely clear on this, but do you mean the "Maintenance DB" selection in pgAdmin III?
Select the server in your "object browser" pane; right click -> Properties
The fifth field is "Maintenance DB"

Maintenance db field is read-only , you can't change it.So you should keep your server properties somewhere and create new server with these properties and set maintenance db "postgres". Now you are able to drop database.

The command line option is :
psql -U intelison -c "UPDATE pg_database SET datistemplate=false, datallowconn=true WHERE datname = '<your_database_name>'"

Related

SQL Server backup to Azure stopped working after moving DB files

I have a database in SQL Server 2014 on premises. For that database I have a backup to Azure storage configured using smart_admin.sp_set_db_backup procedure.
Recently I had to move the database files from one disk to another. I detached database, moved files, reattached it.
After that my backup stopped working. The function smart_admin.fn_backup_db_config shows that database backup record exists but database marked as is_dropped = 1
Any attempt to enable or disable backup for this database fails with error:
SQL Server Managed Backup to Windows Azure cannot configure the database, 'DATABASE_NAME', because it either does not exist or is offline.
Any way I can remove backup configuration information and create a new one? One of the ideas I found is rename the database, but I cannot do that in production.
Vad's answer is close, but there can be more than one record in autoadmin_managed_databases for a given db_name. You need to get the last record, which is the one with the max autoadmin_id. I picked the wrong one, and SQL Server repopulated the drop_date after I ran smart_admin.sp_set_db_backup or the 15 minute interval ran.
use msdb;
go
update [dbo].[autoadmin_managed_databases]
set drop_date = null
where [autoadmin_id]= (select max(autoadmin_id)
from [dbo].[autoadmin_managed_databases]
where db_name = '<DROPPPED_DATABASE_NAME>')
go
Managed Backups - is_dropped flag set to Yes after detaching database
and reattaching DB
Rename the database and set up managed backup again.
Reference
As I mentioned earlier I was not allowed to rename the database on the Production. So I found where it's marked as dropped and changed the value. That helped. Automated backups for the database started working again. IMO what happened looks like a bug in SQL Server 2014.
use msdb;
go
update [dbo].[autoadmin_managed_databases]
set drop_date = null
where [db_name] = '<DROPPED_DATABASE_NAME>'
go

OpenERP 6.1 Database Migration to new VPS through shell

I've been trying to migrate my old openerp server installation to a new VPS so I tried to migrate the database.
I need to do it via shell because of the size of the database and unstable connection.
What I've done is log in server1 and then
su postgres
pg_dump dbname > db.dump
then I transfered the file to the new server and restored it like this
createdb dbname
psql dbname < db.dump
the database itself was restored and I can browse through the tables if I want to but when I try to get in OpenERP the database is not available in the select box where the databases are. If I create new databases by using the openerp interface they appear correctly in the select box and I can connect.
I tried to create the db with UTF8 encoding and using template1 as well but nothing was different. I also tried to create the database via the interface, drop the tables and restore the backup but this gives errors after I log in like "product.product relation does not exist".
Any ideas what else I could try? Thanks in advance.
When restoring the database take care to restore it with the correct ownership.
You may want to take a look at this question

Best (easiest) way to make a SQL Server dump and import that dump in another SQL Server

I would like to achieve a database export (dump) in SQL Server from one server and import that dump in another SQL Server and not necessarily in the same schema name.
For example if I have a database prepared with all the data set for implement a new DB for a new customer, that db is for example named DB_EMPTY
And then I have to setup the same DB on some external server for a customer for example in the schema DB_MY_CUSTOMER
What is the best/simplest way to export (dump) a DB_EMPTY, and import it in DB_MY_CUSTOMER?
Possibly with SQL Server Management Studio?
An easy way would be to use SQL Server Management Studio, in the Object Explorer right click on the database you want to export, select Tasks -> Back Up, then select a destination and file name in the Destination box at the bottom of the dialog. You can play around with the various settings, but you don't need to.
To restore it on another server is basically the opposite, choose Tasks -> Restore -> Database, in the dialog select From Device, then click the browse ellipsis, from there you get a browse dialog, click Add and then navigate to the back up file you created. You can change the databse name in the To database textbox, and control where the files get stored by going to the Options tab and altering the Restore As column entries in the table labelled as Restore the database files as:.
Just in case someone is ending here and noticing "Tasks -> Back up" option does not exists; on recent versions of SSMS ( v18 for example ), you'll need to use "Generate scripts" option and then on "Advanced" select "Schema and Data" on "Types of data to script".
Credits to: http://statmap.co.uk/?page_id=9207
This is called replication : http://databases.about.com/od/sqlserver/ht/distribution.htm
Try to look to the snapshot replication. It is configurable to determine the destination database.

copy a database within SQL Server Express?

I would like to make a copy of a database I have but keep it on the same server as a test database. However, everything I have found is to use the copy database wizard (I am using MS SQL Server Express).
The instructions always say: In SQL Server Management Studio, in Object Explorer, expand Databases, right-click a database, point to Tasks, and then click Copy Database.
I don't have the Copy Database option. I am running as an admin, so no clue why it is missing for me - is it something I have to install separately? I can't do the Detach/Attach since it is copying to the same server. I tried detaching, copying the MDF/LDF, renaming, attaching but as you can imagine that messed a ton up :) I am not great with SQL to do it all programatically. Is there a tool out there I could use?
In SSMS 2008 you can do this:
Create a backup of the database you want to copy
In SSMS, right-click 'Databases' and select 'Restore Database'
Select the database you wish to copy from the 'From database' drop-down list in the 'Source for restore' section
Enter the name of the new database in the 'To database' field in the 'Destination for Restore' section - this cannot be the name of an existing database.
Click OK
You're done! :)
In SQL Server Express 2012 you can do following steps:
Create a backup of the database you want to copy
right-click "Databases" and select "Restore Files and Filegroups"
Enter the name of the new database in the "To database" field.
Select "From device" and then select the file that you backuped in the first step
click "OK"
this will "clone" the Database with the correct table settings such as the "default value" and "auto increase" etc.
SQL Express database has an export button, I just exported the database to a new database on the same server, it is copying the database. Just right-click on the database name.
Take these steps to make a copy of the database in SQL Express
Stop SQL
Copy the mdf, ldf and any other file for the db to a NEW location (make sure you get the log file)
Change the name of each copied file
Start SQL
Right-click Database in SSMQ and select attach
Make sure you change the name in the column "Attach As"
Update the file location in the lower pane of "Database Details" to the location of your copied files (especially that log file)
I was able to copy a database on my SQL Express system with this method
I had a problem creating a copy of my database as well using SQL Express 2012.
I have solved it by the backup and restore method.
After making the backup I used: restore -> files and file groups
Next step was to write a new name for the new database and set the source:
Pointing the source file
and finally, a overwrite the existing database with replace must be selected
and set names for new files with extension mdf and ldf that are different from the existing where is: Restore as
This method worked for me
Just be aware if you are using SQL Server Express 2012 of going to the option Files and make sure that the destination files (Restore As column) are different from the original files *.mdf and *.log
( I tried to put an image but need 10 reputation :p)
I do not believe the Express version of the manager will have the copy database feature. Have you considered copying via the backup and restore method?
http://msdn.microsoft.com/en-us/library/ms190436.aspx
I found the problem! Click on Databases, restore, then do the following:
After choosing from where to restore, and writing destination db name, go to files [annotation 1 on picture] and change the very right column files names to different than original [annotation 2 on picture] then it works :)
I just thought of a really nifty way to get around this :) So I thought I should post my idea. Note that this is 'untested' but I think it will work.
Do a "Back Up..." database (theoretically this is on your
production server, but it doesn't have to be)
Copy the backup file (from your prod server) onto your development machine
Assuming you're using SSMS Developer Edition on your development
machine, you can then do a "Restore" onto your development machine,
then do a "Copy Database" afterwards also on your development
machine (to create a new copy of the DB)
Now do a "Back Up..." on the new DB you just created, copy the backup file (to your production server) and do a "Restore" on the sql server express server :)
Hope this helps out a few people :)
Cheers,
Jeff
The solution is definitely to create a backup and restore it, but ensure that you're restored copy is pointing to different .mdf and .ldf files.
Here's how to check that using SSMS 2014 and a SQL Server 12 installation: assuming you're creating and restoring backups on your local disk.
Create a backup of your existing database
Right click the database, and choose "back up..." under "tasks."
(If you leave the location as the default, you don't have to hunt for the back up in the next step when you restore.)
Restore your backup to a NEW database:
Right click on databases, choose "restore database"
Select "device"
Click the ellipsis button ("...") to open the "Selct Backup devices" dialog.
Choose "File" as the backup media type and click the "add" button
Select the backup you just made, click ok (twice)
Now, back in the "restore database" dialog, type a new name for your destination database
Click "files" under "select a page" and make sure that "restore as" is pointing to .mdf and .ldf file names that do not already exist
Click ok!
I think you could try import data to a new database.
Create an empty database in your local sql server
Right click on the new database -> tasks -> import data
In the SQL Server Import and Export Wizard, select product env's servername as data source. And select your new database as the destination data.
As for the first part of your question (why do you not see this Copy Database Wizard option under Tasks for your dbs), the answer is indeed in the fact that you are running SQL Server Express. SQL Server Express doesn't support the SQL Server Agent feature, which this Copy DB feature relies upon, so the Copy DB feature is not shown (but many online resources fail to make that observation).
Fortunately, most everyone else has addressed the second part of your question (how to achieve the task otherwise), and nearly all point out using the existing backup and restore options (which DO exist in Express), or the export/import (which also exists in Express, I can confirm).
Try making a backup of your database, and restoring it into a brand new database.
Create new DB.
Make a full backup of your original.
Right click on your new DB, hit restore.
Navigate to your .BAK, and ensure the .mdf and .ldf match the new.

Trying to restore via t-sql script. Exclusive access could not be obtained because the database is in use

I'm trying to restore backups of our production databases to our development server. When I run the following script which has previously worked:
RESTORE DATABASE M2MDATA01 FROM DISK = 'C:\Install\SQLBackup\M2MDATA01.SQLBackup' WITH REPLACE,
MOVE 'M2MDATA01' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\M2MData01.mdf',
MOVE 'M2MDATA01_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\M2MData01.ldf'
I get the following error:
Error 12/21/2009 9:06:09 AM 0:00:00.000 SQL Server Database Error: Exclusive access could not be obtained because the database is in use. 5 0
However, I have no idea how what could possibly be using it. How can I tell?
Check what's connected (the easy way)
SELECT * FROM sys.sysprocesses S WHERE S.dbid = DB_ID('M2MDATA01')
Note: sysprocesses can not be emulated using dmvs...
Edit, check for locks too
SELECT * FROM sys.dm_tran_locks L WHERE L.resource_type = 'DATABASE' AND L.resource_database_id = DB_ID('M2MDATA01')
In SQL Server Management Studio, go to Management => Activity Monitor. This will show you all processes that are connected to all databases, and allow you to kill these processes (only recommended as a last resort).
Profiler is one option.
From Sql Server Management Studio, select Tools|Sql Server Profiler.
Connect to the server instance that the database you are working with is on.
Switch to the Event Selection tab
check the checkbox below the grid labeled "Show all columns"
In the grid find the DatabaseName column and check the entire column.
(optional) press the Column Filters button and filter to the database name you are working with.
This should at least tell you if something is using the database in question.
Easiest solution here is to delete the database and select the "close existing connections" checkbox before hitting okay. Then the restore will work just fine. It's just DEV right? :}
If you're restoring a db, do you really care who is connected? Or what is being done with those connections? I would think not. Just "kick everyone out" by setting the database into single user mode and then restore the database.
USER master
GO
ALTER DATABASE M2MDATA01
SET SINGLE_USER
--This rolls back all uncommitted transactions in the db.
WITH ROLLBACK IMMEDIATE
GO
RESTORE DATABASE M2MDATA01
FROM DISK = 'C:\Install\SQLBackup\M2MDATA01.SQLBackup'
WITH REPLACE,
MOVE 'M2MDATA01' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\M2MData01.mdf',
MOVE 'M2MDATA01_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\M2MData01.ldf'
GO
Now, one additional item to be aware of. After you set the db into single user mode, someone else may attempt to connect to the db. If they succeed, you won't be able to proceed with your restore. It's a race! My suggestion is to run all three statements at once in a single batch.

Resources