postgreSQL.app : create database

2020-06-24 17:15发布

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

2条回答
成全新的幸福
2楼-- · 2020-06-24 17:43

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
        }
    }
    
查看更多
▲ chillily
3楼-- · 2020-06-24 17:43

The reason help had no effect is that you were already in the middle of writing a command. SQL commands must be terminated by a semicolon. Pay attention to the psql prompt - see how it changed from =# to -#? That indicates you're in the middle of a command. See In psql, why do some commands have no effect? .

If you weren't halfway though a command, typing help would've shown:

mydbname=> help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

Now... here's the manual for the psql command.

For summary help on psql its self use \? .

For a listing of SQL commands use \h.

For help on specific commands use \h COMMAND NAME e.g. \h CREATE DATABASE to see how to use the CREATE DATABASE command. For more details on a command read the manual, e.g. the manual on CREATE DATABASE.

Here's the PostgreSQL tutorial, which covers getting started.

查看更多
登录 后发表回答