Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

django - postgreSQL.app : create database

Hello I'm new in postgreSQL,Please guide me a bit

I have a django project

here is settings.py :

DATABASES = {
"default": {
    "ENGINE": "django.db.backends.postgresql_psycopg2",
    "NAME": "testfor_psl",
    "USER": "",
    "PASSWORD": "",
    "HOST": "localhost",
    "PORT": "",
    }
}

And I run python manage.py syncdb

There is error:
OperationalError: FATAL: database "testfor_psl" does not exist

So how can I create db??

I use posgreSQL.app, and click the Open psql

There is a terminal like this :

I type help,and nothing happen.
Please help me. Thanks

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to put ; at the end of psql commad. As you can see, after command

winsome=# CREATE DATABASE testfor_psl

the prompt is changed from =# to -#. It means, that psql still wait for the command to be completed by providing ;.

Also, it is better to create a database user for django project. So here what you need to do:

  1. Create user in database (in psql):

    CREATE USER testfor_psl_user WITH password 'pass';
    
  2. Create database with owner equals to that user:

    CREATE DATABASE testfor_psl ENCODING 'UTF8' TEMPLATE template0 OWNER testfor_psl_user;
    
  3. Set credentials in django project settings:

    DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "testfor_psl",
        "USER": "testfor_psl_user",
        "PASSWORD": "pass",
        "HOST": "localhost",
        "PORT": "5432",  # default port
        }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...