आपके द्वारा जोड़े गए बाधा के साथ (सुपरसियर नहीं होने के कारण), मुझे शुद्ध-एसक्यूएल समाधान नहीं मिला है। लेकिन इसे अपनी पसंदीदा भाषा में करना काफी सरल है। आप "पुराने" डेटाबेस से कनेक्शन खोलते हैं, दूसरा डेटाबेस में एक और, आप एक में और INSERT में दूसरे में चयन करते हैं। पाइथन में एक परीक्षण और कामकाजी समाधान यहां दिया गया है।
#!/usr/bin/python
"""
Copy a *part* of a database to another one. See
<http://stackoverflow.com/questions/414849/whats-the-best-way-to-copy-a-subset-of-a-tables-rows-from-one-database-to-anoth>
With PostgreSQL, the only pure-SQL solution is to use COPY, which is
not available to the ordinary user.
Stephane Bortzmeyer <[email protected]>
"""
table_name = "Tests"
# List here the columns you want to copy. Yes, "*" would be simpler
# but also more brittle.
names = ["id", "uuid", "date", "domain", "broken", "spf"]
constraint = "date > '2009-01-01'"
import psycopg2
old_db = psycopg2.connect("dbname=dnswitness-spf")
new_db = psycopg2.connect("dbname=essais")
old_cursor = old_db.cursor()
old_cursor.execute("""SET TRANSACTION READ ONLY""") # Security
new_cursor = new_db.cursor()
old_cursor.execute("""SELECT %s FROM %s WHERE %s """ % \
(",".join(names), table_name, constraint))
print "%i rows retrieved" % old_cursor.rowcount
new_cursor.execute("""BEGIN""")
placeholders = []
namesandvalues = {}
for name in names:
placeholders.append("%%(%s)s" % name)
for row in old_cursor.fetchall():
i = 0
for name in names:
namesandvalues[name] = row[i]
i = i + 1
command = "INSERT INTO %s (%s) VALUES (%s)" % \
(table_name, ",".join(names), ",".join(placeholders))
new_cursor.execute(command, namesandvalues)
new_cursor.execute("""COMMIT""")
old_cursor.close()
new_cursor.close()
old_db.close()
new_db.close()
मैं इस मिल:
यदि आप किसी भी कस्टम mytable पर चलाता है है, तो आप आयात पर उन्हें निष्क्रिय करने की आवश्यकता हो सकती करने के लिए या एक फ़ाइल से कॉपी करने के लिए होना चाहिए सुपर उपयोगकर्ता ... है इसके चारों ओर किसी भी तरह, यह मानते हुए कि मैं सुपरसियर के रूप में मनमाने ढंग से कोड चलाने के लिए नहीं जा रहा हूँ? – mike
माइकल बुआज़ जैसे उत्तरों से बचने के लिए आपको इस प्रतिबंध को जोड़ने के लिए मूल प्रश्न को संपादित करना चाहिए। – bortzmeyer