Ubuntu に,PostgreSQL 12 のインストールを行う. そして,データベース生成,テーブル定義を行ってみる.
※ インストールは,https://wiki.postgresql.org/wiki/Apt に記載の手順に従う.
【このページの目次】
サイト内の関連ページ: データベースのことを含む各種教材は別ページにまとめている.
参考Webページ:
Ubuntu で OS のシステム更新を行うときは, 端末で,次のコマンドを実行.
sudo apt update sudo apt -yV upgrade sudo /sbin/shutdown -r now
https://wiki.postgresql.org/wiki/Apt に記載の手順に従う.
sudo apt -y install curl ca-certificates gnupg curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt -y update
sudo apt -y install postgresql-12
このとき,「PostgreSQL サービスアカウント」の権限で,PostgreSQL データベースサーバを起動する.
パスワードは,「自分のパスワード」を入れること.
sudo -u postgres service postgresql start sudo service postgresql stop
なお,Ubuntu の起動のとき,PostgreSQL は自動で起動する.
/sbin/shutdown -r now
PostgreSQL サービスアカウントは,Postgres サーバの起動等に使うもの.
Ubuntu でパッケージを使って PostgreSQL をインストールすると,PostgreSQL サービスアカウント(ユーザ名は「postgres」)が自動的に作成される.
次のコマンドで確認できる.
sudo cat /etc/passwd | grep postgres
※ 「x」は no password という意味(パスワードがないという意味ではない)
PostgreSQL サービスアカウントは Linux が管理するアカウントのこと.PostgreSQL が管理するアカウントとは別のものである.
このとき,「PostgreSQL サービスアカウント」の権限で,PostgreSQL データベースサーバを起動する.
パスワードは,「自分のパスワード」を入れること.
sudo -u postgres service postgresql restart service postgresql status
sudo service postgresql stop service postgresql status
データベースファイルを置くディレクトリのこと. 好きに決めていいが,日本語を含まないディレクトリ名にすること. ここでは,次のように設定する.
ここでは,データベースディレクトリは /var/lib/postgresql/data とする.
ファイルの所有者は,「PostgreSQL サービスアカウント」にする.
sudo mkdir /var/lib/postgresql/data sudo chown -R postgres:postgres /var/lib/postgresql/data
sudo -u postgres /usr/lib/postgresql/12/bin/initdb --encoding='UTF-8' -D /var/lib/postgresql/data
sudo -u postgres psql -U postgres
SQL の create database コマンドを使用.
文字コード(エンコーディング)UTF8で, データベース名 testdb のデータベースを作成したいときは, 次のように操作する.
create database testdb owner postgres encoding 'UTF8';
\q
PostgreSQL の場合,データベースの一覧表示は,「psql -l」で行う.
sudo -u postgres psql -U postgres -l
sudo -u postgres psql -U postgres -d testdb
create table order_records (
id integer primary key not null,
year integer not null CHECK ( year > 2008 ),
month integer not null CHECK ( month >= 1 AND month <= 12 ),
day integer not null CHECK ( day >= 1 AND day <= 31 ),
customer_name text not null,
product_name text not null,
unit_price real not null check ( unit_price > 0 ),
qty integer not null default 1 check ( qty > 0 ),
created_at timestamp with time zone not null default current_timestamp,
updated_at timestamp with time zone not null default current_timestamp,
check ( ( unit_price * qty ) < 200000 ) );
begin transaction; insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2020, 7, 26, 'kaneko', 'orange A', 1.2, 10 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2020, 7, 26, 'miyamoto', 'Apple M', 2.5, 2 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2020, 7, 27, 'kaneko', 'orange B', 1.2, 8 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2020, 7, 28, 'miyamoto', 'Apple L', 3 ); commit;
select * from order_records;
begin transaction; update order_records set unit_price = 11.2 where id = 1; commit; select * from order_records;
\d
\q
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ) ![]()