Mit der Einschränkung hinzugefügt (nicht Superuser), finde ich keine reine SQL-Lösung. Aber es in Ihrer Lieblingssprache zu tun ist ziemlich einfach. Sie öffnen eine Verbindung zur "alten" Datenbank, eine andere zur neuen Datenbank, Sie SELECT in der einen und INSERT in der anderen. Hier ist eine getestete und funktionierende Lösung in Python.
#!/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()
Ich erhalte diese: ERROR: muss Superuser sein, um zu kopieren oder aus einer Datei ... gibt es egal wie es geht, vorausgesetzt, ich werde keinen willkürlichen Code als Superuser ausführen können? – mike
Sie sollten die ursprüngliche Frage bearbeiten, um diese Einschränkung hinzuzufügen, um Antworten wie Michael Buen zu vermeiden. – bortzmeyer