I want to drop all of the databases except few ones.
Lets say there are 20 databases and I want to delete 18 out of them but keep 2 as it is the latest ones and are in use.
Please suggest.
First, execute the following query in the psql terminal.
select 'drop database "'||datname||'";'
from pg_database
where datistemplate=false;
This will generate drop database command for all the databases. Copy the result in a text editor and exclude(delete) what you want to keep and save it as dd.sql file. And execute it like this:
psql -d postgres -f dd.sql
From pgAdmin you can now select properties on a database, select DBs to drop and click delete/drop. Quick and easy! Drop selected databases:
As accepted answer kinda demonstrates it, dropping multiple databases was particularly tedious for me, so I wrote an helper script to alleviate this operation : https://github.com/Kraymer/ezdropdb
In short, you enter a pattern that the databases you want to suppress must match then all db names results are listed and there is a final prompt where you can enter which ones of those to drop (cf screenshot on project page) .
Related
I have a postgresql database for code (flask, ember) that is being developed. I did a db_dump to back up the existing data. Then I added a column in the code. I have to create the database again so the new column will be in the database. When I try to restore the data with psql -d dbname -f dumpfile I get many errors such as 'relation "xx" already exists', " violates foreign key constraint", etc.
I'm new to this. Is there a way to restore old data to a new empty database that has all the relationships set up already? Or do I have add a column "by hand" to the database when I add a column in the code, to keep the data?
The correct way to proceed is to use ALTER TABLE to add a column to the table.
When you upgrade code, you can simply replace the old code with new one. Not so with a database, because it holds state. You will have to provide SQL statements that modify the existing database so that it changes to the desired new state.
To keep this manageable, use specialized software like Flyway or Liquibase.
When you did the pg_dump, you only dumped the data and table structure, bit did not drop any tables. Now, you are trying to restore the dump, and that will attempt to re-create the tables.
You have a couple options (the first is what I'd recommend):
Add --clean to your pg_dump command -- this will DROP all the tables when you go to restore the dump file.
You can also --data-only your pg_dump command -- this will only dump the existing data, and will not attempt to re-create the tables. However, you will have to find a way to truncate your tables (or delete the data out of them) so as not to encounter any FK errors or PK collisions.
I'm used to scripting in Python or Matlab, and my first couple hours with SQL have been infuriating. I would like to make a list of columns appear on the screen in any way, shape, or form; but when I use commands like
select *
from "2Second Log.dbo.TagTable.Columns"
I keep getting the error:
Invalid column name '[the first column in my table]'.
even though I never explicitly asked for [the first column in my table], it found it for me. How can you correctly identify the first column name, and then still claim it's invalid!? Babies will be strangled.
This db was generated by Allen Bradley's FactoryTalk software. What I would really like to do is produce an actual list of "TagName" strings...but I get the same error when I try that. If there were a way to actually double click the table and open it up and look at it (like in Matlab), that would be ideal.
Echoing juergen's suggestion in the comment above. It looks like you're running the query on the master database, not the 2Second Log database that actually has your table. (You can tell this by looking at the database in the dropdown in the top left of your screenshot). Two things you can do:
Change the dropdown in the top left to 2Second Log. This will target your query to a different database
Put your database name in brackets as suggested by juergen i.e. select * from [2Second Log].dbo.TagTable
As an side, if you're looking for a good SQL tutorial, I highly recommend the Mode SQL tutorial. It's a fantastic interactive platform to get your SQL feet wet.
always use brackets when names/field have spaces or dashes.
select * from [2Second Log].dbo.TagTable
Scenario: I'm in IntelliJ IDEA DB console and looking at
SELECT * FROM TableXY;
I want to see the definition of the TableXY. One way of doing it is:
ctrl+click on the table name: Looks up the table in the Database window.
F4: Opens the table editor.
select the Text tab
The problem is that I'm on a DB with a lot of tables and the first step takes forever because IDEA loads the full list of tables.
Is there a way to jump to the table editor directly?
I am not sure if this is what you are looking for, but to quickly view the table definition you can use the Quick Documentation pop-up:
Place your cursor within the table name and hit CTRL+Q (or F1 on Mac). This will show you some information about the table, the first rows, and the table definition (output from SHOW CREATE TABLE).
You can also configure the Quick Documentation under Settings > Tools > Database (see Intellij IDEA on-line help).
I have one database with an image table that contains just over 37,000 records. Each record contains an image in the form of binary data. I need to get all of those 37,000 records into another database containing the same table and schema that has about 12,500 records. I need to insert these images into the database with an IF NOT EXISTS approach to make sure that there are no duplicates when I am done.
I tried exporting the data into excel and format it into a script. (I have doe this before with other tables.) The thing is, excel does not support binary data.
I also tried the "generate scripts" wizard in SSMS which did not work because the .sql file was well over 18GB and my PC could not handle it.
Is there some other SQL tool to be able to do this? I have Googled for hours but to no avail. Thanks for your help!
I have used SQL Workbench/J for this.
You can either use WbExport and WbImport through text files (the binary data will be written as separate files and the text file contains the filename).
Or you can use WbCopy to copy the data directly without intermediate files.
To achieve your "if not exists" approache you could use the update/insert mode, although that would change existing row.
I don't think there is a "insert only if it does not exist mode", but you should be able to achieve this by defining a unique index and ignore errors (although that wouldn't be really fast, but should be OK for that small number of rows).
If the "exists" check is more complicated, you could copy the data into a staging table in the target database, and then use SQL to merge that into the real table.
Why don't you try the 'Export data' feature? This should work.
Right click on the source database, select 'Tasks' and then 'Export data'. Then follow the instructions. You can also save the settings and execute the task on a regular basis.
Also, the bcp.exe utility could work to read data from one database and insert into another.
However, I would recommend using the first method.
Update: In order to avoid duplicates you have to be able to compare images. Unfortunately, you cannot compare images directly. But you could cast them to varbinary(max) for comparison.
So here's my advice:
1. Copy the table to the new database under the name tmp_images
2. use the merge command to insert new images only.
INSERT INTO DB1.dbo.table_name
SELECT * FROM DB2.dbo.table_name
WHERE column_name NOT IN
(
SELECT column_name FROM DB1.dbo.table_name
)
How do I dump single table data from a huge dump file into the database.
If I understand your question correctly - you already have a dump file of many tables and you only need the restore one table (right?).
I think the only way to do that is to actually restore the whole file into a new DB, then copy the data from the new DB to the existing one, OR dump only the table you just restored from the new DB using:
mysqldump -u username -p db_name table_name > dump.sql
And restore it again wherever you need it.
To make things a little quicker and save some disk, you can kill the first restore operation after the desired table was completely restored, so I hope the table name begins with one of the first letters of the alphabet :)
There are some suggestions on how you might do this in the following articles:
http://blog.tsheets.com/2008/tips-tricks/mysql-restoring-a-single-table-from-a-huge-mysqldump-file.html
http://blog.tsheets.com/2008/tips-tricks/extract-a-single-table-from-a-mysqldump-file.html
I found these by searching for "load single table from mysql database dump" on Google: http://www.google.com/search?q=load+single+table+from+mysql=database+dump