2012-03-21 70 views
0

我最近更改了我的项目中的数据库服务器从sqlite3到Postgresql,我有几个问题希望能给出我的问题的答案。Django从sqlite3切换到Postgresql问题

  1. 据我所知,从sqlite切换到Postgres意味着我创建了新的数据库和表格,对吧?我已经这样做了,但是我还没有看到在我的项目中创建的任何新文件显示我所创建的数据库是可见的。 (顺便说一句,我已经改变了settings.py中的数据库名称)

  2. 我可能应该提及我在虚拟环境中工作,我想知道是否会以任何方式影响我的引用。我试图导入Django中的表来尝试统计表中的记录数,但是我得到错误:“没有名为psdemo的模块”。 (psdemo是我的数据库名称,我试图与导入表:

    from ps.psdemo import Product 
    

    其中Ps是我的应用程序,psdemo是我的数据库和产品数据库中的表

在。结论我试图访问我的数据库和表,但我无法找到它们。我再说一遍,在我的项目或虚拟环境中没有新的数据库文件(我已经彻底搜索过),但是如果我使用终端连接我可以连接到我的虚拟环境并更改目录以进入应用程序文件夹,然后如果我连接到Postgresql服务器,我可以创建数据库,表s并且可以插入它们,进行查询等,但是我无法从Django代码访问它们。

回答

2

I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)

所有你需要做的事情就是创建数据库。不是桌子。一旦你调用syncdb,Django将创建将创建表,其他任何它认为有用的表。

你不会像在sqlite中那样在你的项目中有任何新文件。如果你想查看你的数据库,你应该下载并安装pgadminIII(这我会建议在任何情况下)

I probably should mention that I'm working in a virtual environment and I would like to know if that affects my references in any way. I've tried to import the tables in Django to try and count the number of records in a table but I get the error: "No module named psdemo". (psdemo is my database name and i'm trying to import the table with:

在这里,你通过正常的Python语法导入模型,然后将它引用您的表。每个模型应该代表一个表。你先定义你的模型,然后调用

python manage.py syncdb 

In conclusion I'm trying to get access to my database and tables but I can't manage to find them.

见上面,但你绝对应该阅读有关从Postgres的文档Postgres的安装,并阅读psycopg2文档以及为建立一个Django文档postgres数据库。

+0

如果我能,我会upvote你的答案,但讨厌downvote我的问题。非常感谢你! – 2012-03-21 14:56:47

2

I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)

数据库文件不是在postgresql的项目目录中创建的。它们在数据库服务器数据目录中创建(如/ var/lib/postgres,它取决于分发)。您通常应通过连接到PostgreSQL服务器的PostgreSQL客户端来查询它,而不是直接与文件混淆。

例如,您可以运行命令:

manage.py dbshell 
+0

好吧,我已经运行了命令,它将我连接到我用来连接的服务器。有关如何访问我的数据的任何其他提示? – 2012-03-21 14:22:17

+0

您可以使用pg_dump命令进行导出,使用pg_restore命令进行恢复,或者使用manage.py loaddata/dumpdata,或者只使用一些模型,而django会处理其余的问题! – jpic 2012-03-21 14:24:16

+0

我犯了一个小错误,并放弃了我的psdemo数据库,现在看来我无法连接到dbshel​​l了。 – 2012-03-21 14:42:41

0

关于你的第一个问题,请@ JPIC的答案。

在第二个问题上,您的数据库不是包,而且也不会从数据库导入模型。如果您在进行任何更改之前能够正确导入模型,则会将您的导入语句改回原样。