3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ubuntu 22.04 で PostgreSQL の使い方

Last updated at Posted at 2022-08-21

インストール

sudo apt install postgresql-14 postgresql-client-14

サーバーが動いているのを確認

sudo systemctl status postgresql
$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr>
     Active: active (exited) since Mon 2022-08-22 08:11:16 JST; 22min ago
    Process: 21703 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 21703 (code=exited, status=0/SUCCESS)
        CPU: 2ms

ユーザーを postgres に切り替え

postgres のパスワードを設定

sudo passwd postgres
su postgres

データベースの作成

createdb city

psql を立ち上げる

psql
$ psql
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
Type "help" for help.

postgres=# 

簡単なコマンドを入力して psql の動作確認

postgres=# select version();
postgres=# select current_date;

ユーザーの作成

postgres=# create role scott with login password 'tiger123';

ユーザーの確認

postgres=# \du

psql から抜ける

postgres=# exit

データベースを指定して、psql を起動

psql city
$ psql city
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
Type "help" for help.

city=# 

データベース city へのアクセス許可を scott に与える

city=# grant all on all tables in schema public to scott;

scott で データベース city に接続

psql -h localhost -U scott -d city
$ psql -h localhost -U scott -d city
Password for user scott: 
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

city=>

SQL を使う

テーブルの作成

psql -h localhost -U scott -d city < postgre_create.sql
postgre_create.sql
drop table cities;
create table cities (id varchar(10) primary key, name text, population int, date_mod date);

データの挿入

psql -h localhost -U scott -d city < postgre_insert.sql
postgre_insert.sql
insert into cities values ('t3461','広島',72814,'2001-9-14');
insert into cities values ('t3462','福山',41738,'2001-7-21');
insert into cities values ('t3463','東広島',92513,'2001-6-12');
insert into cities values ('t3464','呉',93167,'2001-9-29');
insert into cities values ('t3465','尾道',95419,'2001-3-18');
insert into cities values ('t3466','竹原',82314,'2001-2-21');
insert into cities values ('t3467','三次',76152,'2001-8-16');
insert into cities values ('t3468','大竹',37541,'2001-7-7');
insert into cities values ('t3469','府中',46518,'2001-10-9');

データを読む

psql -h localhost -U scott -d city < postgre_read.sql
postgre_read.sql
select * from cities;

データベースのテーブルを表示

export PGPASSWORD=tiger123
psql -h localhost -U scott -d city < list.sql
list.sql
\dt

データベースのテーブルのレコード数を表示

export PGPASSWORD=tiger123
psql -h localhost -U scott -d city < list_count.sql
list_count.sql
select relname, n_live_tup from pg_stat_user_tables where schemaname='public';
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?