Ubuntu で psql のインストール
sudo apt install postgresql-contrib
データベースが動いていることを確認
$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; prese>
Active: active (exited) since Fri 2024-05-10 08:41:07 JST; 5h 26min ago
バージョンの確認
$ psql --version
psql (PostgreSQL) 16.4 (Ubuntu 16.4-0ubuntu0.24.04.2)
psql の起動
$ sudo -u postgres psql
psql (16.2 (Ubuntu 16.2-1ubuntu4), server 14.8 (Ubuntu 14.8-0ubuntu0.22.10.1))
Type "help" for help.
postgres=#
データベースの一覧
postgres=# \l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | ICUロケール | ロケールプロバイダー | アクセス権限
-----------+----------+------------------+-------------+-------------------+-------------+----------------------+-----------------------
city | scott | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | | libc |
postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | | libc |
template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
(4 行)
データベースの切り替え
postgres=# \c city
データベース"city"にユーザー"postgres"として接続しました。
city=#
テーブルの一覧
city=# \dt
リレーション一覧
スキーマ | 名前 | タイプ | 所有者
----------+--------+----------+--------
public | cities | テーブル | scott
(1 行)
テーブル定義の表示
city=# \d cities
テーブル"public.cities"
列 | タイプ | 照合順序 | Null 値を許容 | デフォルト
------------+-----------------------+----------+---------------+------------
id | character varying(10) | | not null |
name | text | | |
population | integer | | |
date_mod | date | | |
インデックス:
"cities_pkey" PRIMARY KEY, btree (id)
データの表示
city=# select * from cities;
id | name | population | date_mod
-------+--------+------------+------------
t3461 | 広島 | 72814 | 2001-09-14
t3462 | 福山 | 41738 | 2001-07-21
t3463 | 東広島 | 92513 | 2001-06-12
t3464 | 呉 | 93167 | 2001-09-29
t3465 | 尾道 | 95419 | 2001-03-18
t3466 | 竹原 | 82314 | 2001-02-21
t3467 | 三次 | 76152 | 2001-08-16
t3468 | 大竹 | 37541 | 2001-07-07
t3469 | 府中 | 46518 | 2001-10-09
(9 rows)
テーブルの削除
city=# drop table cities;
DROP TABLE
ユーザーの作り方
# CREATE ROLE scott login password 'tiger123';
CREATE ROLE
データベースの作り方
sudo -u postgres createdb city
データのダンプ
pg_dump -h 'localhost' -U scott -d city > ./example.dump
データのリストア
psql -h 'localhost' -U scott -d city < ./example.dump
テーブルの削除
$ psql -U scott city
psql (14.2 (Ubuntu 14.2-1ubuntu1), server 13.6 (Ubuntu 13.6-0ubuntu0.21.10.1))
Type "help" for help.
city=> drop table cities;
DROP TABLE
city=> exit
データベースの削除
$ sudo -u postgres psql
psql (15.2)
"help"でヘルプを表示します。
postgres=# drop database city;
DROP DATABASE
所有者が scott の city というデータベースを作成
$ sudo -u postgres psql
psql (15.2)
"help"でヘルプを表示します。
postgres=# create database city;
CREATE DATABASE
postgres=# alter database city owner to scott;
ALTER DATABASE
SQL
データベース内のテーブルのレコード数の一覧
select reltuples, relname FROM pg_class ORDER BY reltuples DESC;
pg_hba.conf の場所を探す
postgres=# show hba_file;
hba_file
-------------------------------------
/etc/postgresql/13/main/pg_hba.conf
(1 row)
postgres=#
Password 認証をするには、pg_hba.conf を編集する必要があります。
/etc/postgresql/16/main/pg_hba.conf
(省略)
local all all md5
(省略)
スキーマ
スキーマ一覧
city=# \dn
List of schemas
Name | Owner
--------+----------
city | scott
public | postgres
sch001 | scott
(3 rows)
スキーマ変更
city=# set search_path=sch001;
SET
現在のスキーマの表示
city=# select current_schema();
current_schema
----------------
sch001
(1 row)
スキーマの削除
city=# drop schema sch002;
DROP SCHEMA
空でないスキーマの削除
city=# drop schema sch001 cascade;
NOTICE: drop cascades to 6 other objects
DETAIL: drop cascades to table table_a
drop cascades to table table_b
drop cascades to table table_p
drop cascades to table table_q
drop cascades to table table_r
drop cascades to table table_s
DROP SCHEMA