LoginSignup
10
15

More than 5 years have passed since last update.

Djangoで使うDBをSQLiteからPostgreSQLに変更する手順

Last updated at Posted at 2018-12-17

はじめに

個人的にDB変更する際に時間かかったので、手順を残しておく。
筆者はPython以外全て初めて触るレベル。Macすら触り始めて1週間。

環境

name version
macOS 10.13.4
Python 3.6.5
Django 2.1.3
PostgreSQL 11.1

postgreSQLのインストール

brew経由でインストールする。

install_command
brew install postgresql

完了したら正常にインストールされたか、バージョンチェックする。

version_check_command
psql --version
psql (PostgreSQL) 11.1

Pythonのデータベースバインディングのインストール

データベースバインディングが何かは知らんが、必要らしいのでインストール。

install_command
pip install psycopg2

DBの設定

init db

DBの初期化。以下、"duchida"ってなってるところは自分のユーザー名に置き換えてください。

init_db
% initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "duchida".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

起動してみる

以下のログでればおっけーっぽい。

start_server
% pg_ctl -D /usr/local/var/postgres start
waiting for server to start....2018-12-12 20:44:33.569 JST [33393] LOG:  listening on IPv6 address "::1", port 5432
2018-12-12 20:44:33.569 JST [33393] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2018-12-12 20:44:33.571 JST [33393] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-12-12 20:44:33.592 JST [33394] LOG:  database system was shut down at 2018-12-12 20:43:31 JST
2018-12-12 20:44:33.598 JST [33393] LOG:  database system is ready to accept connections
 done
server started

DB list 取得

起動できてると以下のような、コンソール表示になるはず。

get_db_list
% psql -l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges
-----------+---------+----------+-------------+-------------+---------------------
 postgres  | duchida | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | duchida | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/duchida         +
           |         |          |             |             | duchida=CTc/duchida
 template1 | duchida | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/duchida         +
           |         |          |             |             | duchida=CTc/duchida
(3 rows)

環境変数設定

PGDATAを設定するとpg_ctlのオプション指定が楽になる。

shell profileは自分の環境に読み替えてください。

.zprofile
export PGDATA=/usr/local/var/postgres
pg_ctl_start
% pg_ctl start
waiting for server to start....2018-12-12 20:52:47.759 JST [33887] LOG:  listening on IPv6 address "::1", port 5432
2018-12-12 20:52:47.759 JST [33887] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2018-12-12 20:52:47.761 JST [33887] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-12-12 20:52:47.782 JST [33888] LOG:  database system was shut down at 2018-12-12 20:51:41 JST
2018-12-12 20:52:47.789 JST [33887] LOG:  database system is ready to accept connections
 done
server started
pg_ctl_stop
% pg_ctl stop
waiting for server to shut down....2018-12-12 20:53:55.686 JST [33887] LOG:  received fast shutdown request
2018-12-12 20:53:55.687 JST [33887] LOG:  aborting any active transactions
2018-12-12 20:53:55.687 JST [33887] LOG:  background worker "logical replication launcher" (PID 33894) exited with exit code 1
2018-12-12 20:53:55.688 JST [33889] LOG:  shutting down
2018-12-12 20:53:55.704 JST [33887] LOG:  database system is shut down
 done
server stopped

PGDATAを設定していない場合、以下のコマンドになる。

pg_ctl -D /usr/local/var/postgres start
pg_ctl -D /usr/local/var/postgres stop

ユーザーの追加と確認

ユーザー作成

インストールしたてだとrootしかいないのでDB使うユーザーを追加する。

create_user
% createuser -P djangodb_user
Enter password for new role:
Enter it again:

ユーザーの確認

ちゃんと追加されてるか確認する

UserList
% psql -q -c'select * from pg_user' djangodb_user
    usename    | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig
---------------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 duchida       |       10 | t           | t        | t       | t            | ******** |          |
 djangodb_user |    16384 | f           | f        | f       | f            | ******** |          |
(2 rows)

DB作成

新しいDBを作る。

createdb
% createdb django-db -O djangodb_user

作成できたか確認する

% psql -l
                                   List of databases
   Name    |     Owner     | Encoding |   Collate   |    Ctype    |  Access privileges
-----------+---------------+----------+-------------+-------------+---------------------
 django-db | djangodb_user | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | duchida       | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | duchida       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/duchida         +
           |               |          |             |             | duchida=CTc/duchida
 template1 | duchida       | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/duchida         +
           |               |          |             |             | duchida=CTc/duchida

DB接続

"DB名=>"ってなれば成功

connect_command
% psql -U djangodb_user django-db
psql (11.1)
Type "help" for help.

django-db=>

Django側の設定

settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'DBNAME', #作ったDB名
        'USER': 'USERNAME', #作ったユーザー名
        'PASSWORD': 'PASSWORD', #ユーザー作った時のパスワード
        'HOST': 'localhost',
        'PORT': '',
    }
}

あとはmigrateして成功すればOK

おわりに

MacOSアップデートして10.14.2にしたらいろいろ動かなくなって泣いてる

10
15
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
10
15