将Django从SQLite转换为PostgreSQL [英] Converting Django from SQLite to PostgreSQL

查看:61
本文介绍了将Django从SQLite转换为PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Django的新手.我已经购买了一本书,例如Django 3(第3版),并且非常喜欢这些课程.我现在进入第3章,目标是增强搜索引擎功能.为此,本书提供了将数据库转换为PostgreSQL的步骤.我正在按照以下说明进行操作,并且尝试了几种方法来解决在Google上发现的问题.我似乎找不到答案.

I'm new to Python and Django. I've purchased a book, Django 3 by example (3rd Edition) and really enjoying the lessons. I'm in chapter 3 now and the goal is to enhance the search engine functionality. In order to do so, the book has steps to convert the DB to PostgreSQL. I'm following the directions below and I've tried several things to fix the issue that I've found on Google. I can't seem to find the answer.

步骤:

  1. 安装PostgreSQL:sudo apt-get install postgresql postgresql-contrib
  2. 安装psycopg2:pip安装psycopg2-binary == 2.8.4
  3. 为PostgreSQL数据库创建用户.打开外壳并运行以下命令.a)su postgresb)createuser -dP博客
  4. 输入新用户的密码,然后创建博客数据库,并使用以下命令为刚创建的博客用户授予所有权:createdb -E utf8 -U博客Blog
  5. 编辑settings.py文件...省略了步骤.

我被困在上面的步骤3a中.我被要求输入postgres的密码,但似乎无济于事.我也尝试过在Google上找到的一些东西.这是我尝试过的.

I'm stuck on step 3a above. I get asked for a password for postgres and nothing seems to work. I have tried some things found on google as well. Here is what I've tried.

sudo -u postgres psql
\password postgres
enter and confirm password
\q

尝试使用su postgres登录并获得su身份验证错误.

Try to log back in using su postgres and get a su authentication error.

推荐答案

我能找到的最佳资源是

The best resource I could find about it is the digital ocean link about how to do this configuration in ubuntu.

此处总结一些步骤:

  • 登录到psql命令行界面,以将角色从sudo用户更改为postgres,然后打开psql命令行提示符.

  • Login to psql command line interface, in order to do that change role from sudo user to postgres, and open psql command line prompt.

user@ubuntu$ sudo -i -u postgres
postgres@ubuntu# psql

  • 在psql内部创建一个供Django应用程序使用的新用户

  • Inside psql create a new user for use in django application

    CREATE ROLE djangouser WITH ENCRYPTED PASSWORD '&%mysupersecurepasswordthatc4ndiff!culh4ck3rsbruteforce!$';
    

  • 授予djangouser用户登录权限

  • Grant privileges to user djangouser to login

    ALTER ROLE "djangouser" WITH LOGIN; --use double quotes here
    

  • 创建一个数据库来保存您的数据

  • create a the database to hold your data

    CREATE DATABASE blog;
    

  • 在数据库博客中授予数据库用户 djangouser

     GRANT ALL PRIVILEGES ON DATABASE blog TO djangouser;
    

  • 现在您的postgres数据库已正确配置.

    Now your postgres database is configured properly.

    如果您在创建新角色时遇到任何问题,请参见文档用于创建角色命令.

    If you have any problems to create a new role, here is the documentation for create role command.

    现在,您可以收集有关数据库名称,用户和密码的信息,以使用适当的值来配置settings.py来组合连接字符串.

    Now you can gather information about database name, user and password to configure your settings.py with proper value for assemble your connection string.

    这篇关于将Django从SQLite转换为PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆