2

I am following a tutorial from the book:

Mele, Antonio. Django 2 by Example: Build powerful and reliable Python web applications from scratch (Kindle Locations 1917-1918). Packt Publishing. Kindle Edition.

I'm at this part:

  • Adding full-text search to your blog
    • Installing PostgreSQL

I'm on Windows 10 and I installed Postgres fine. The instructions say to type in the postgres shell:

su postgres
createuser -dP blog

I guess I'm trying to create a user called blog that will have a password and be allowed to create databases?

When I do that I get:

Server [localhost]: su postgres
Database [postgres]: createuser -dP blog
Port [5432]:
Username [postgres]:
psql: warning: extra command-line argument "postgres" ignored
psql: warning: extra command-line argument "-d" ignored
psql: warning: extra command-line argument "createuser" ignored
psql: warning: extra command-line argument "-dP" ignored
psql: warning: extra command-line argument "blog" ignored
psql: warning: extra command-line argument "-p" ignored
psql: warning: extra command-line argument "5432" ignored
psql: could not translate host name "su" to address: Unknown host
Press any key to continue . . .

I'm not sure what to do or what exactly is going on? The instruction is pretty unclear

1
  • The book is supposed to work on any OS I think it's just an oversight on the part of the author - it just says to open the shell. I could use pgAdmin and I have a Mac I use postgres on, but I would like to know what's going on with the windows psql shell? Commented Dec 7, 2018 at 17:14

3 Answers 3

2

It looks like you're trying to use commands in psql, su postgres and createuser -dP blog, that are meant for a bash like shell. su postgres says to switch the the postgres OS user, and the createuser command is a shell command to create database users. However, if you are in psql that command isn't accessible.

It also appears that you're typing those commands in when psql is asking for database connection info, so even if you were sending it sql/psql commands it wouldn't work at that point.

Here is the PostgreSQL Documentation on the createuser command. Use this command from the system shell to create database users.

Here is the PostgreSQL Documentation on CREATE ROLE. This is how you create users from within psql.

Sign up to request clarification or add additional context in comments.

Comments

1

createuser is a command to be given from the command line. Since you are on Windows that means it is a .exe file probably located in the bin directory of wherever you installed Postgres. At the command prompt or powershell prompt use createuser -dP blog.

Additionally you may have to provide -h localhost -p 5432 -U postgres -W password to createuser.exe to enable it to communicate with the server.

Comments

1

I did as I'd done when I was reading Django for Professionals by William S. Vincent.

Once you clicked on PostgreSQL's install it will ask you for a password. Type: postgres When it finishes installing the database go to settings.py of your project, comment out the older one:

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }

and paste the text:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

That's it! I've saved your time! Now you owe me!

1 Comment

PORT : 5432 and ENGINE:'django.db.backends.postgres_psycopg2' - without psycopg2 many unwanted error you can face !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.