Django PostgreSQL安装和设置-程序员宅基地

技术标签: python  postgresql  linux  mysql  数据库  

We’ll know that SQLite is very powerful, embedded relational database management system and it offers a really amazing set of tools to manage approximately all sorts of data. But when it comes Multi-user applications (where multiple users want to use the same databases),  It fails. So It is recommended to choose a fully featured relational database management system rather than SQLite.

我们将知道SQLite是非常强大的嵌入式关系数据库管理系统,它提供了一套非常了不起的工具来管理几乎所有类型的数据。 但是,当涉及到多用户应用程序(多个用户希望使用相同的数据库)时,它就会失败。 因此,建议选择功能全面的关系数据库管理系统,而不要选择SQLite。

Some examples of Fully featured RDBMS are MySQL, PostgreSQL, Oracle databases.

功能齐全的RDBMS的一些示例是MySQL,PostgreSQL,Oracle数据库。

Django is very flexible in terms of changing databases.

Django在更改数据库方面非常灵活。

So in this article, we’ll see how we can change the database from SQLite to PostgreSQL.

因此,在本文中,我们将看到如何将数据库从SQLite更改为PostgreSQL。

先决条件 (Prerequisites)

Instead of creating everything from scratch, we’re taking the example of our previous article, in which we’re using SQLite database.

而不是从头开始创建所有内容,我们以上一篇文章为例,其中我们使用SQLite数据库。

If you haven’t read our previous article yet, then here it is https://www.thecrazyprogrammer.com/2019/01/django-models.html

如果您尚未阅读我们的上一篇文章,那么这里是https://www.thecrazyprogrammer.com/2019/01/django-models.html

Let’s start.

开始吧。

步骤1:安装PostgreSQL (Step 1: Install PostgreSQL)

To Install PostgreSQL for Django in Ubuntu:

要在Ubuntu中为Django安装PostgreSQL:

Open terminal and type these two commands.

打开终端并键入这两个命令。

sudo apt-get update

sudo apt-get更新

sudo apt-get install python-pip python-dev libpq-dev postgresql       postgresql-         contrib

须藤apt-get install python-pip python-dev libpq-dev postgresql postgresql- contrib

To Install PostgreSQL for Django in Windows:

要在Windows中为Django安装PostgreSQL:

Download the installer from its official site: https://www.postgresql.org/download/windows/

从官方网站下载安装程序: https : //www.postgresql.org/download/windows/

And follow the instructions mentioned in the link. While installing you have to enter a password and a port number. Note it down.

并按照链接中提到的说明进行操作。 在安装时,您必须输入密码和端口号。 记下来。

To Install PostgreSQL for Django in Mac OS:

在Mac OS中为Django安装PostgreSQL:

Like windows, you need to download the installer from its official site: https://www.postgresql.org/download/macosx/

与Windows一样,您需要从其官方网站下载安装程序: https : //www.postgresql.org/download/macosx/

While installing you have to enter a password and a port number. Note it down.

在安装时,您必须输入密码和端口号。 记下来。

步骤2:设置用户和数据库 (Step 2: Setup User and Database)

Login using your password.

使用密码登录。

Here I am using a Ubuntu 18.04 and I am accessing the PostgreSQL from the terminal, So I have to switch to the user postgres that was created while installing PostgreSQL.

在这里,我使用的是Ubuntu 18.04,并且正在从终端访问PostgreSQL,因此,我必须切换到安装PostgreSQL时创建的用户postgres

To switch the user to postgres, open terminal and type

要将用户切换到postgres,请打开终端并输入

sudo su – postgres

sudo su – Postgres

Django PostgreSQL 1

Now you’re ready to enter the shell session for the postgres user. Log into a Postgres session by typing:

现在您可以为postgres用户输入shell会话了。 通过键入以下内容登录到Postgres会话:

psql

Django PostgreSQL 2

psql

Now create a database by typing:

现在通过键入以下内容创建数据库:

CREATE DATABASE myproject;

创建数据库myproject ;

Django PostgreSQL 3

Choose the database name that is more relevant to your project. As it is just an example so I am using here myproject.

选择与您的项目更相关的数据库名称。 因为这只是一个示例,所以我在这里使用myproject。

Now create a user to use the database that we’ve just created. To create a user type:

现在创建一个用户来使用我们刚刚创建的数据库。 要创建用户类型:

CREATE USER myuser WITH PASSWORD ‘mypassword’;

使用密码“ mypassword”创建用户myuser;

Django PostgreSQL 4

So our user and database are created.

这样就创建了我们的用户和数据库。

Now, at last, give the rights to the user to access the database type:

现在,最后,授予用户访问数据库类型的权限:

GRANT ALL PRIVILEGES ON DATABASE myproject TO myuser;

将数据库myproject上的所有特权授予 myuser;

Django PostgreSQL 5

Now just type \q to get back to the postgres user’s shell session then type exit.

现在只需键入\ q返回postgres用户的shell会话,然后键入exit。

Django PostgreSQL 6

That’s all about database setup.

这就是关于数据库设置的一切。

步骤4:安装psycopg2 (Step 4 : Install psycopg2)

psycopg2 is a most popular PostgreSQL database adapter to work with Python. To install pycopg2 type:

psycopg2是最流行的PostgreSQL数据库适配器,可与Python一起使用。 要安装pycopg2,请输入:

pip install django psycopg2

pip安装Django psycopg2

Django PostgreSQL 7

Note: I am using pip3 instead of pip because I have both the versions installed in my Ubuntu Linux.

注意:我使用的是pip3而不是pip,因为我在Ubuntu Linux中都安装了这两个版本。

步骤5:编辑专案的设定档案 ( Step 5: Edit Your Project’s Settings File)

Open your project’s setting file in write mode and go to the DATABASES section.

在写入模式下打开项目的设置文件,然后转到DATABASES部分。

Django PostgreSQL 8

Here you can see that sqlite3 is connected with our project. To change it to PostgreSQL just change these lines as:

在这里,您可以看到sqlite3与我们的项目连接。 要将其更改为PostgreSQL,只需将这些行更改为:

DATABASES = {

数据库= {

    ‘default’: {

'默认':{

        ‘ENGINE’: ‘django.db.backends.postgresql’,

'ENGINE':'django.db.backends.postgresql',

        ‘NAME’: ‘myproject’,

'NAME':'myproject',

        ‘USER’: ‘myuser,

'USER':' myuser

        ‘PASSWORD’ : ‘mypassword’,

'PASSWORD':'mypassword',

        ‘HOST’ : ‘localhost’,

'HOST':'localhost',

        ‘PORT’ : ‘5432’

'PORT':'5432'

    }

}

}

}

Django PostgreSQL 9

In the above code, NAME, USER, PASSWORD are name of database, name of your user and password that we’ve created while creating the user.

在上面的代码中,NAME,USER,PASSWORD是数据库的名称,用户名和我们在创建用户时创建的密码。

And PORT is same number that I recommended to note down while installing the PostgreSQL.

而且PORT是我建议在安装PostgreSQL时记下的编号。

步骤6:迁移专案 (Step 6: Migrate Your Project)

So we’ve installed PostgreSQL and configured the settings.py file. Now last step is to migrate your project.

因此,我们已经安装了PostgreSQL并配置了settings.py文件。 现在,最后一步是迁移您的项目。

Open your project directory in terminal and type:

在终端中打开您的项目目录,然后键入:

python manage.py runserver

python manage.py运行服务器

Django PostgreSQL 10

All set, test and run your project.

全部设置,测试和运行您的项目。

That’s all about changing database from SQLite to PostgreSQL.

这就是将数据库从SQLite更改为PostgreSQL全部内容。

Note: While migrating, if you’re facing a warning like:

注意:在迁移时,如果您遇到以下警告:

UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use “pip install psycopg2-binary” instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.

用户警告:psycopg2 wheel软件包将从2.8版重命名; 为了保持二进制安装,请改用“ pip install psycopg2-binary”。 有关详细信息,请参见:<http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>。

  “””)

“””)

You can solve this issue with installing psycopg2-binary  by using following command:

您可以使用以下命令通过安装psycopg2-binary解决此问题:

pip install psycopg2-binary

pip安装psycopg2-binary

If you’ve any query related to Django PostgreSQL installation and setup, please let us know in the comment box.

如果您有关于Django PostgreSQL安装和设置的任何疑问,请在评论框中告诉我们。

翻译自: https://www.thecrazyprogrammer.com/2019/01/django-postgresql-installation-and-setup.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/culing2941/article/details/108617342

智能推荐

怎么样将java打包成jar包_java如何打包成压缩后jar包-程序员宅基地

文章浏览阅读2.3k次,点赞2次,收藏10次。使用工具:eclipse步骤:1、运行eclipse2、选择需要打包成jar包的java文件3、右击选择-->Export4、选择Java下面的JAR file-->next5、勾选如下图,然后Next-->Next注意:JAR file输入框中需要填写打包好的jar文件存放的目录6、确认需要打包的文件,如下图选择好之后,点击fi_java如何打包成压缩后jar包

Python封装了很好用的结构和方法,为啥还要学数据结构?_python封装好的数据结构-程序员宅基地

文章浏览阅读7.9k次,点赞83次,收藏416次。大家前面学过Python基础知识的都知道,Python为我们封装了列表、字典等高级数据类型,并且他们都带有一系列增、删、改、除的方法,让我们能够很方便的处理一些问题。以目前我们这些人的技术水平可能觉得这些东西就够了,照样能够快速的解决很多的问题。可是随着知识的深..._python封装好的数据结构

计算机应用能力window,计算机应用能力考试教材:Windows XP操作系统(题库版)...-程序员宅基地

文章浏览阅读199次。计算机应用能力考试教材:Windows XP操作系统(题库版)语音编辑锁定讨论上传视频《计算机应用能力考试教材:WindowsXP操作系统(题库版)》是2011年电子工业出版社出版的图书,作者是全国专业技术人员计算机应用能力考试专家委员会。书名计算机应用能力考试教材:Windows XP操作系统(题库版)作者全国专业技术人员计算机应用能力考试专家委员会ISBN97871211364..._计算机应用能力考试windows xp

MONGODB(四)——DBObject与JavaBean转换-程序员宅基地

文章浏览阅读404次。一、DBObject 转为 JavaBean /** * 将实体Bean对象转换成DBObject * */ public static <T> DBObject beanToDBObject(T bean) throws IllegalArgumentException, IllegalA..._mongodb dbobject转javabean

在vscode中引用uni-app扩展组件(uni-ui),不用手动导入_vscode uniapp 自动引入组件easycom-程序员宅基地

文章浏览阅读903次。一、在src下的pages.json文件里面写入easycom规则。三、接下来运行项目就能使用啦,不用手动导入组件。二、npm下载uni-ui。_vscode uniapp 自动引入组件easycom

计算机科学导论第8章答案,第8章计算机科学导论.ppt-程序员宅基地

文章浏览阅读304次。第8章计算机科学导论计算机科学导论 1. 经典压缩工具WinRAR 它采用了独特的多媒体压缩算法和紧固式压缩法,这点更是针对性地提高了其压缩率。 它默认的压缩格式为RAR,该格式压缩率要比ZIP格式高出10%~30%,同时它也支持ZIP、ARJ、CAB、LZH、ACE、TAR、GZ、UUE、BZ2、JAR类型压缩文件。 8.5.4 各种实用工具软件 (1) 压缩文件 在“常规”标签项中输..._计算机科学导论第四版第八章答案

随便推点

人工智能 -- NLP:文本去掉停用词stopwords_nlp 去除停用词-程序员宅基地

文章浏览阅读4.2k次。人工智能:文本去掉停用词stopwords为了彻底搞懂本质,写的非常细!(1)准备停用词import pandas as pdimport jieba# 准备停用词stopwords = pd.read_csv("data/stopwords.txt", index_col=False, quoting=3, sep='\t', names=['word'], encoding='u..._nlp 去除停用词

MFC-vs资源试图添加窗口以及类视图添加窗口消息_类视图消息-程序员宅基地

文章浏览阅读504次。vs资源试图添加窗口可修改ID,会自动创建对于ID数据的,在.h文件中enum { IDD = IDD_XXX};中可以使用类视图添加窗口消息_类视图消息

JRebel2023.3 插件使用详解-程序员宅基地

文章浏览阅读10w+次,点赞413次,收藏1.3k次。简介JRebel是一套JavaEE开发工具。Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率。JRebel是一款JAVA虚拟机插件,它使得JAVA程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响。JRebel使你能即时分别看到代码、类和资源的变化,你可以一个个地上传而不是一次性全部部署。当程序员在开发环境中对任何一个类或者资源作出修改的时候,这..._jrebel

MT7525 ONU调试指令详解_echo pll_reset > /proc/pon_phy/debug-程序员宅基地

文章浏览阅读2.8k次。tr69抓包调试:loglevel set tr69c Debuglogdest set tr69c Telnetsoapdebug enablesave2、开启WAN接口 debugloglevel set wanmgr Debug3、omci 调试命令(必须接串口):echo msg err 1 &amp;gt; /proc/gpon/debugecho msg oam 1..._echo pll_reset > /proc/pon_phy/debug

Java流程控制语句_java if else销量统计,记得把每一次面试当做经验积累英语-程序员宅基地

文章浏览阅读762次,点赞30次,收藏26次。在该程序中,当录入第 3 门课的成绩时,录入的成绩为负数,判断条件“score

最全自学黑客技术学习路线,少走弯路_黑客技术自学-程序员宅基地

文章浏览阅读3.5w次,点赞45次,收藏167次。谈起黑客,可能各位都会想到:盗号,其实不尽然;黑客是一群喜爱研究技术的群体,在黑客圈中,一般分为三大圈:娱乐圈 技术圈 职业圈。 娱乐圈:主要是初中生和高中生较多,玩网恋,人气,空间,建站收徒玩赚钱,技术高的也是有的,只是很少见。 技术圈:这个圈子里面的黑客是为了能把黑客技术玩到极致的技术狂人,我最佩服的就是这群人,希望以后自己也能成为这样的人。 职业圈:这里面的人群主要就是玩HC为主了,行走于黑白两道之间,富的一批。 好了,回到正题,本篇文章主要是写新的“黑客技术学习路线”,前面的文章就当Beta_黑客技术自学