Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value is missing user/password/host information #320

Closed
grondman opened this issue Jul 28, 2020 · 3 comments
Closed

Default value is missing user/password/host information #320

grondman opened this issue Jul 28, 2020 · 3 comments

Comments

@grondman
Copy link

Location within the Book

  • Book build date (Is the date on page 1): 2020-07-11
  • Book format (PDF, Epub or Mobi): PDF
  • Chapter or Appendix: 18
  • Section: 4

Description

Section 18.4 contains a snippet from base.py saying it should contain postgres://user:passwd@localhost:5432/everycheese, but here I get postgres:///everycheese which actually seems to be correct when I look at the GitHub source code of django-crash-starter, as it contains the template "postgres:///{{cookiecutter.project_slug}}" which is consistent with the user/password/host information missing...

Possible Solutions

I don't know if this is something to fix in the book or in the django-crash-starter project?

Your full name so we can provide accurate credit within the book

Ivo Grondman

@luzfcb
Copy link
Contributor

luzfcb commented Jul 28, 2020

@grondman Thanks to open a issue.

Please follow the steps below to get it resolved.

Please let me know if you have any more trouble.

1 - Install the dependencies inside the everycheese conda environment ( conda activate everycheese ):
The command below must be executed inside the project folder.

pip install -r requirements/local.txt

2 - Ensure that in your "everycheese/config/settings/base.py" contains this [1] content before the # GENERAL

[1] https://gist.github.com/luzfcb/96515096c777b360d7b13aad2239fdec

(the latest version of django-crash-starter already includes the content of the previous file.)

3 - On the root directory of your everycheese project, create a file named .env (that is, the .env file must be in the same directory as the manage.py file)

touch .env

4 - add the following configuration to the .env file and save the changes.

DATABASE_URL=postgres://myuser:mypasswd@localhost:5432/everycheese

5 - Edit the .gitignore file and add .env and save the changes.

6 - Run python manage.py sqlcreate

You should get this output:

CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypasswd' CREATEDB;
CREATE DATABASE everycheese WITH ENCODING 'UTF-8' OWNER "myuser";
GRANT ALL PRIVILEGES ON DATABASE everycheese TO myuser;


7 - Execute the command below to create the database user with the given password, create the database and link the user to the newly created database.

On Linux:

python manage.py sqlcreate | sudo -u postgres psql -U postgres

On Mac:

python manage.py sqlcreate | psql

Troubleshooting

If for some reason you get an error similar to this:

ERROR:  syntax error at or near "WITH"
LINE 1: CREATE USER  WITH ENCRYPTED PASSWORD '' CREATEDB;
                     ^
ERROR:  zero-length delimited identifier at or near """"
LINE 1: CREATE DATABASE everycheese WITH ENCODING 'UTF-8' OWNER "";
                                                             ^
ERROR:  syntax error at or near ";"
LINE 1: GRANT ALL PRIVILEGES ON DATABASE everycheese TO ;

Or

ERROR:  role "myuser" already exists
ERROR:  database "everycheese" already exists
GRANT

You can delete the database and the user with the commands below and then perform step 7 again.

⚠️ Be very careful here!: The commands below erase data, and should only be executed on your local development machine and NEVER on a production server.

On Linux:

sudo -u postgres dropdb -U postgres --if-exists everycheese
sudo -u postgres dropuser -U postgres --if-exists myuser

On Mac:

dropdb --if-exists everycheese
dropuser --if-exists myuser

@bummmer
Copy link

bummmer commented Aug 2, 2020

This worked, thank you.

@hlsautter
Copy link

tested this process on macOS Catalina

(everycheese) hsautter@MacBook everycheese % touch .env
(everycheese) hsautter@MacBook everycheese % python manage.py sqlcreate
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypasswd' CREATEDB;
CREATE DATABASE everycheese WITH ENCODING 'UTF-8' OWNER "myuser";
GRANT ALL PRIVILEGES ON DATABASE everycheese TO myuser;
(everycheese) hsautter@MacBook everycheese %

@pydanny pydanny closed this as completed Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants