勉強用に、PythonでPostgresqlを制御しようと思います。
その前に、Postgresqlの設定と、マイグレーションをしようかと思います。
まずyoyo-migrationsを使用します。
yoyo-migrationsとは
yoyo-migrationsとは、データベースのスキーマを管理するツールです。
他にもありますが、なんとなくシンプルそうだったので採用しました。
PostgreSQLのインストール
使用しているディストリビューションはubuntu 20.04です。
公式を見ながらインストールしました。
$ sudo apt-get install curl ca-certificates gnupg
$ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt install postgresql-common
$ sudo sh /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
$ sudo apt update
$ sudo apt install postgresql-13
### 起動と起動確認
$ sudo systemctl start postgresql
$ sudo systemctl status postgresql
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: enabled)
Active: active (exited) since Sat 2020-11-14 10:19:15 JST; 5s ago
Process: 102819 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 102819 (code=exited, status=0/SUCCESS)
11月 14 10:19:15 ubuntu systemd[1]: Starting PostgreSQL RDBMS...
11月 14 10:19:15 ubuntu systemd[1]: Finished PostgreSQL RDBMS.
ユーザ作成とデータベース作成と
これらまでは手動で行います。
データベースと、ユーザ名は以下を設定します。
データベース名: testdb
ユーザ名: testuser
パスワード: testuserp
データベースの文字コード(エンコーディング): UTF-8
以下でtestdbをtestuserでログインできるようにします。
$ sudo su - postgres $ vi /etc/postgres/13/pg_hba.conf local testdb testuser md5 $ psql > create role testuser with LOGIN PASSWORD 'testuserp'; > create database testdb owner postgres encoding 'UTF8'; > GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;
yoyo-migrationsの設定
やっと本題です。
テーブルは以下を作成します。
create table addr ( id SERIAL, name text, addr text, ipaddr inet, create_time timestamp with time zone, update_time timestamp with time zone, );
本稿のソース
ここに設定したソースを置いてあります。
インストール及び設定
yoyoの設定ファイルは以下の通りです。
[DEFAULT] sources = %(here)s/migrations database = postgresql://testuser:testuserp@localhost/testdb verbosity = 3 batch_mode = on prefix = testp_
以下のように起動します。
$ pipenv install yoyo-migrations $ pipenv run yoyo -c ./migration/yoyo.ini list STATUS ID SOURCE -------- ---------------- ------------------------------------------------------------------------ U 0001_create_addr /home/tanino/script-plactice/yoyo-migrations-sample/migration/migrations $ pipenv run yoyo -c ./migration/yoyo.ini apply INFO:yoyo.migrations:Applying 0001_create_addr INFO:yoyo.migrations: - applying step 0 DEBUG:yoyo.migrations: - executing b'create table addr (id SERIAL, name text, addr text, ipaddr inet, create_time timestamp with time zone, update_time timestamp with time zone);' INFO:yoyo.migrations:Marking 0001_create_addr applied
確認
出来たかどうか確認するのは以下です。
postgres@ubuntu:~$ psql -d testdb
testdb=# \d addr
テーブル"public.addr"
列 | タイプ | 照合順序 | Null 値を許容 | デフォルト
-------------+--------------------------+----------+---------------+----------------------------------
id | integer | | not null | nextval('addr_id_seq'::regclass)
name | text | | |
addr | text | | |
ipaddr | inet | | |
create_time | timestamp with time zone | | |
update_time | timestamp with time zone | | |
終わりに
yoyo-migrations自体はうまくいきました。
次はfastapi + sqlalchemyでAPI(CRUDアプリケーションを作ってみたいと思います。