0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PostgreSQLのインストール方法、データベース作成、ユーザー作成、Djangoとの接続準備

Last updated at Posted at 2019-08-09

初めましてjackです。
8月からPythonをメインにエンジニア業務に従事することになりまして。
PostgreSQLを触り始めたので立ち上がり方法を簡単にメモメモ。

使用環境

  • macOS

では早速、インストールから。

brew install postgresql

インストールが出来たら試しにデータベースを起動させてみましょう。

postgres -D /usr/local/var/postgres

データベース作成

データベースを起動させた状態でデータベースを作成しましょう

createdb データベース名

削除する場合は

dropdb データベース名

作成したデータベースにアクセスするには

psql データベース名

この後Djangoに接続させる為のユーザーを作成します。

ユーザー作成

CREATE USER ユーザー名;

そしてデータベースの管理者をユーザーに設定します。

ALTER DATABASE データベース名 OWNER TO ユーザー名;

Djangoとの接続準備

次にDjangoとの接続方法を記載します。
作成したDjangoのアプリのsettings.pyの下記編集が必要です。

settings.py
DATABASES = {
    'default': {'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'データベース名',
        'USER':'ユーザー名',
        'PASSWORD':'',
        'HOST':'localhost',
        'PORT':5432
    }
python3 manage.py startproject アプリ名

を行ったアプリ名をsettings.pyのINSTALED_APPSに追加。
最後のターミナルで下記操作すればOK!

python3 manage.py makemigrations アプリ名
python3 manage.py migrate
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?