psql の使い方
4748 ワード
データベースが動いていることを確認
$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr>
Active: active (exited) since Sun 2021-10-31 14:34:11 JST; 30min ago
バージョンの確認
$ psql --version
psql (PostgreSQL) 13.4 (Ubuntu 13.4-1)
psql の起動
$ sudo -u postgres psql
psql (13.4 (Ubuntu 13.4-1))
Type "help" for help.
postgres=#
データベースの一覧
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
city | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
データベースの切り替え
postgres=# \c city
psql (13.4 (Ubuntu 13.4-1), server 12.6 (Ubuntu 12.6-0ubuntu0.20.10.1))
You are now connected to database "city" as user "postgres".
city=#
テーブルの一覧
city=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+-------
public | cities | table | scott
(1 row)
テーブル定義の表示
city=# \d cities
Table "public.cities"
Column | Type | Collation | Nullable | Default
------------+-----------------------+-----------+----------+---------
id | character varying(10) | | not null |
name | character varying(20) | | |
population | integer | | |
date_mod | character varying(20) | | |
Indexes:
"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 (13.4 (Ubuntu 13.4-1), server 12.6 (Ubuntu 12.6-0ubuntu0.20.10.1))
Type "help" for help.
city=> drop table cities;
DROP TABLE
city=> exit
Author And Source
この問題について(psql の使い方), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/2659daa31d75d3f5939c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .