0

Two things to note: One, I am executing these commands with my virtual environment running. So terminal appears as (env):. Two, every time I tried to enter su postgres, it asked me for a password.

I am reading a book on Django(using a mac), the text says: "If you are using macOS, download PostgreSQL from https://www.postgresql.org/download/ and install it. You also need to install the Psycopg2 PostgreSQL adapter for Python. Run the following command to install it: pip install psycopg2==2.7.4."

I already had postgres installed on my machine, as I was previously fumbling with trying to deploy a website, before reverting back to this example project I had built. So I did not go to the website and install it. I installed psycopg2 as instructed ((env): pip install psycopg2==2.7.4.)

"Let's create a user for our PostgreSQL database. Open the shell and run the following commands: su postgres then createuser -dP blog"

I was thinking that "open the shell" meant enter the python virtual shell? So:

python manage.py shell

then

su postgres

this did not work, it asked me for a password. Exited the shell, ran the same command (env): su postgres, same result.

From there I thought maybe since I am in a virtual environment I do have to redownload postgres. I ran pip install postgres (did not know if this would download something, but it did, so I didn't bother going to the website/also would not know how to install it to this environment from the website). Note, this installed postgres as well as psycopg-someversion-binary. So now I have psycopg2==2.7.4 and some version of psycopg2-binary installed. From there I ran (env): su postgres (note from my environment). The same result, it asked for a password.

Then I reentered my python interactive shell with python manage.py shell and again ran su postgres. Same result.

At this point, I tried to research it on my own. What I read said I need to run su - postgres from root(?) by entering sudo -i at the terminal. I did this, now I am at Justins-MBP:~ root# (this does not seem it is at all related to the django project I am trying to use a postgres db for). Anyway, from here su - postgres and as you might have guessed, it asked me for a password.

Any help on how I can properly switch the db is greatly appreciated.

Also, after typing all of this and thinking about what I did I realized I haven't changed the database settings of the python project yet. But neither has the book, so not really sure how this project would even be aware of this database?

Justin
  • 1,329
  • 3
  • 16
  • 25

1 Answers1

1

You probably have not yet set up the root user on MacOS, see this SO answer. However, for more information about the use of su, see this answer with a link to Apple's tech notes on how to create one.

The reason you should do this in your macOS terminal is because your setting up, configuring and doing database actions on a PostrgreSQL database, and this on its own, in the way your book is describing, is unrelated to Django (or Python for that matter).

So you have to make sure that 1) PostgreSQL is installed on your device and 2) that you have PostgreSQL running (as you will be creating users, which in fact means INSERTing data into your PostgreSQL Database). I used this blogpost (not affiliated) in the past to set up (install, run and configure) PostgreSQL on MacOS.

Regarding Django, you will need to change DATABASES in your settings.py (also see this part in Django tutorial)

DATABASES = {
    # default namespace
    'default': { 
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'dbname' #name of the database,
        'USER': 'username' #name of the created user,
        'PASSWORD': 'password',
        'HOST': '127.0.0.1' #when local,
        'PORT': '5432' #default psql port,
    }
}
  • Thanks, that first link helped me set up the db. Connected it to my django project and now it working(locally). – Justin Feb 13 '20 at 20:53