问题
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.
Steps:
- install PostgreSQL: sudo apt-get install postgresql postgresql-contrib
- install psycopg2: pip install psycopg2-binary==2.8.4
- create user for PostgreSQL database. Open the shell and run the following commands. a) su postgres b) createuser -dP blog
- Enter a password for the new user and then create the blog database and give ownership to the blog user you just created with the following command: createdb -E utf8 -U blog blog
- Edit the settings.py file... steps omitted.
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
Try to log back in using su postgres and get a su authentication error.
回答1:
You are confusing the system user postgres with the database user postgres. sudo -u postgres psql runs a psql session as the system user postgres(the -u) and logs into as the database user postgres. The default database user for psql, if a database user('-U') is not specified, is the system user that starts it. Running \password postgres then creates a password for the database user postgres. Once that is done you can log in via psql from any system user account as the database user postgres. So in your personal user account: psql -d postgres -U postgres. You will be prompted for a password, use the one you created. For more information see Ubuntu Postgres and psql
回答2:
The best resource I could find about it is the digital ocean link about how to do this configuration in ubuntu.
Summing up some steps here:
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# psqlInside psql create a new user for use in django application
CREATE ROLE djangouser WITH ENCRYPTED PASSWORD '&%mysupersecurepasswordthatc4ndiff!culh4ck3rsbruteforce!$';Grant privileges to user djangouser to login
ALTER ROLE "djangouser" WITH LOGIN; --use double quotes herecreate a the database to hold your data
CREATE DATABASE blog;grant privileges in database blog to database user
djangouserGRANT ALL PRIVILEGES ON DATABASE blog TO djangouser;
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.
Now you can gather information about database name, user and password to configure your settings.py with proper value for assemble your connection string.
来源:https://stackoverflow.com/questions/65427026/converting-django-from-sqlite-to-postgresql