I want to copy the datas from one database to another in Postgres. I wrote a script in django and was able to grab a datas from one specific table but how can i add that data in other database.New database has same table and column name, i want to save that old database files to new database. This might be easy for some of you guys but i really couldnt figure that out.
我想在Postgres中将数据从一个数据库复制到另一个数据库。我在django中编写了一个脚本,并且能够从一个特定的表中获取数据,但是如何在其他数据库中添加该数据。新数据库具有相同的表和列名,我想将旧数据库文件保存到新数据库。这对你们中的一些人来说可能很容易,但我真的无法理解这一点。
3 个解决方案
#1
0
I'm not familiar with either API but if the rows/columns have the same dimmensions you could do something like (and this is partially pseudocode):
我不熟悉这两种API,但如果行/列具有相同的dimmensions,你可以做类似的事情(这是部分伪代码):
for x in range(height):
for y in range(width):
data = call_data_from_database_A(x, y)
new_entry = enter_data_into_database_B(x, y)
Where the Call_data is you're getting data from that specific row/column, and enter_data enters the data into that specific row/column. I'm not familiar with either API but if you find the two I'm sure you could figure it out rather quickly.
Call_data是从哪个特定行/列获取数据,而enter_data将数据输入到该特定行/列。我对这两种API都不熟悉,但如果你发现这两种,我相信你可以很快找到它。
#2
0
Instead of writing your own import and export code, why not use the native capabilities of Postgres and dump the table from your old database then import it into your new one:
为什么不使用Postgres的本机功能并从旧数据库中转储表,然后将其导入新数据库,而不是编写自己的导入和导出代码:
http://www.postgresql.org/docs/current/static/sql-copy.html
http://www.postgresql.org/docs/current/static/sql-copy.html
#3
0
The simplest way to do this with Django (move one Django database to another, defined with a different model django database) is to write a 2 Django views and one jquery html page.
使用Django(将一个Django数据库移动到另一个,使用不同的模型django数据库定义)的最简单方法是编写2个Django视图和一个jquery html页面。
The first view will be in the original Django app. It will essentially create a json object model of the database and push it out on a get request. This is custom to your Django's models.
第一个视图将在最初的Django应用程序中。它本质上将创建数据库的json对象模型,并在get请求中将其推出。这是您的Django模型的习惯。
The second view will be in the new Django app. This will take in json data and format it to match your current Django database (fields might not match up exactly, hence the reason for doing this migration). You then just add elements into the new database just as you were creating a new Django model entry(example).
第二个视图将在新的Django应用程序中。这将获取json数据并将其格式化以匹配您当前的Django数据库(字段可能不完全匹配,因此进行此迁移的原因)。然后,就像创建新的Django模型条目一样,只需将元素添加到新数据库中(示例)。
I personally use a one off jquery html page that gets the json data from the first view and posts it to the second one. You could exclude this piece and just write it all in python in the second view, but I find doing it this way to be much cleaner.
我个人使用一个关闭jquery html页面,从第一个视图获取json数据并将其发布到第二个视图。你可以排除这个部分,并在第二个视图中将它全部写在python中,但我觉得这样做可以更清洁。
#1
0
I'm not familiar with either API but if the rows/columns have the same dimmensions you could do something like (and this is partially pseudocode):
我不熟悉这两种API,但如果行/列具有相同的dimmensions,你可以做类似的事情(这是部分伪代码):
for x in range(height):
for y in range(width):
data = call_data_from_database_A(x, y)
new_entry = enter_data_into_database_B(x, y)
Where the Call_data is you're getting data from that specific row/column, and enter_data enters the data into that specific row/column. I'm not familiar with either API but if you find the two I'm sure you could figure it out rather quickly.
Call_data是从哪个特定行/列获取数据,而enter_data将数据输入到该特定行/列。我对这两种API都不熟悉,但如果你发现这两种,我相信你可以很快找到它。
#2
0
Instead of writing your own import and export code, why not use the native capabilities of Postgres and dump the table from your old database then import it into your new one:
为什么不使用Postgres的本机功能并从旧数据库中转储表,然后将其导入新数据库,而不是编写自己的导入和导出代码:
http://www.postgresql.org/docs/current/static/sql-copy.html
http://www.postgresql.org/docs/current/static/sql-copy.html
#3
0
The simplest way to do this with Django (move one Django database to another, defined with a different model django database) is to write a 2 Django views and one jquery html page.
使用Django(将一个Django数据库移动到另一个,使用不同的模型django数据库定义)的最简单方法是编写2个Django视图和一个jquery html页面。
The first view will be in the original Django app. It will essentially create a json object model of the database and push it out on a get request. This is custom to your Django's models.
第一个视图将在最初的Django应用程序中。它本质上将创建数据库的json对象模型,并在get请求中将其推出。这是您的Django模型的习惯。
The second view will be in the new Django app. This will take in json data and format it to match your current Django database (fields might not match up exactly, hence the reason for doing this migration). You then just add elements into the new database just as you were creating a new Django model entry(example).
第二个视图将在新的Django应用程序中。这将获取json数据并将其格式化以匹配您当前的Django数据库(字段可能不完全匹配,因此进行此迁移的原因)。然后,就像创建新的Django模型条目一样,只需将元素添加到新数据库中(示例)。
I personally use a one off jquery html page that gets the json data from the first view and posts it to the second one. You could exclude this piece and just write it all in python in the second view, but I find doing it this way to be much cleaner.
我个人使用一个关闭jquery html页面,从第一个视图获取json数据并将其发布到第二个视图。你可以排除这个部分,并在第二个视图中将它全部写在python中,但我觉得这样做可以更清洁。