LoginSignup
0
0

Debian 12: PostgreSQL 15 をインストール

Last updated at Posted at 2024-02-14

インストール

sudo apt install postgresql
sudo apt install postgresql-client
sudo apt install libpq-dev

PostgreSQL が動いていることを確認

sudo systemctl status postgresql

データベースの作成

postgres ユーザーにパスワードをつける

sudo passwd postgres

postgres ユーザーに切り替える

$ su postgres
Password:

psql を起動

$ psql
could not change directory to "/home/uchida": Permission denied
psql (15.5 (Debian 15.5-0+deb12u1))
Type "help" for help.

postgres=#

ユーザーの作成

ユーザー scott
パスワード tiger123

postgres=# create user scott password 'tiger123';

確認

postgres=# select usename,usesysid from pg_user;
 usename  | usesysid 
----------+----------
 postgres |       10
 scott    |    16399
(2 rows)

データーベースの作成

データベース名 city

postgres=# create database city owner = scott;
CREATE DATABASE

確認

postgres=# \l

ユーザーで接続できるようにする

設定前

$ psql -U scott city
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "scott"

設定の変更

peer を md5 に変更

/etc/postgresql/15/main/pg_hba.conf
(省略)
#local	all	all	peer
local	all	all	md5
(省略)

PostgreSQL を再起動

sudo systemctl restart postgresql

設定後

$ psql -U scott city
Password for user scott: 
psql (15.5 (Debian 15.5-0+deb12u1))
Type "help" for help.

city=>

簡単なテーブル操作

テーブルの作成

create table cities (id varchar(10) primary key, name text, population int, date_mod date);

テーブルの一覧

city=> \dt
        List of relations
 Schema |  Name  | Type  | Owner 
--------+--------+-------+-------
 public | cities | table | scott
(1 row)

city=>

データの挿入

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');

データの表示

select * from cities;
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)
0
0
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
0
0