LoginSignup
8
5

PostgreSQL を Ubuntu に普通にインストール

Last updated at Posted at 2023-04-19

久しぶりに PostgreSQL をインストール。
Ubuntu ローカルの仕組みなどがあったのでメモ。

環境

  • Ubuntu 20.04 LTS
  • Ubuntu 22.04 LTS

インストール

$ sudo apt install postgresql postgresql-contrib

initdb などは必要ない。

ローカルユーザからの操作

postgres ユーザになって psql を起動する

$ sudo -u postgres psql

動作確認

postgres=# select version();
                                                                version                                                                 
----------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.14 (Ubuntu 12.14-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)

postgres=# show LC_COLLATE;
 lc_collate  
-------------
 ja_JP.UTF-8
(1 row)

初期ユーザ postgres は、パスワード無しで設定されている。

もし postgres に パスワードを設定したいときは、psql 内で ALTER コマンドで設定。

# ALTER ROLE postgres WITH password 'hogehoge';

外部から接続を許す

まず、TCP 5432 ポートを LISTEN する設定を行う。

Ubuntu20.04 (Postgres12)の場合、
/etc/postgresql/12/main/postgresql.conf を編集する。

Ubuntu22.04 (Postgres14)の場合、
/etc/postgresql/14/main/postgresql.conf を編集する。

# - Connection Settings -

#listen_addresses = 'localhost'		# what IP address(es) to listen on;
					# comma-separated list of addresses;
					# defaults to 'localhost'; use '*' for all
					# (change requires restart)
port = 5432				# (change requires restart)

↓以下のように書き換える

# - Connection Settings -

listen_addresses = '*'		# what IP address(es) to listen on;
					# comma-separated list of addresses;
					# defaults to 'localhost'; use '*' for all
					# (change requires restart)
port = 5432				# (change requires restart)

また、pg_hba.conf を書き換えて認証を受ける。

Ubuntu20.04 (Postgres12)の場合は /etc/postgresql/12/main/pg_hba.conf
Ubuntu22.04 (Postgres14)の場合は /etc/postgresql/14/main/pg_hba.conf

以下を追加することで、192.168.42.0/24192.168.1.0/24のネットワークから パスワード認証での接続が可能となる

host	all		all		192.168.42.0/24		md5
host	all		all		192.168.1.0/24		md5

以下のようにして反映

$ sudo systemctl restart postgresql.service

外部から接続してみる

Ubuntu22.04 から接続

$ sudo apt install postgresql-client

としてから

$ psql -h 192.168.42.5 -U postgres -d postgres

とすると

Password for user postgres: 
psql (14.7 (Ubuntu 14.7-0ubuntu0.22.04.1), server 12.14 (Ubuntu 12.14-0ubuntu0.20.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=# 

接続できました

ユーザを作る

postgresユーザになってから操作します。

$ sudo -u postgres bash
$ createuser -d -r -s hogehoge
$ psql
postgres=# ALTER USER hogehoge with encrypted password 'fugafuga';

今回はわざわざ コマンドでユーザ作ってから psql でパスワードを設定していますが一度にやってももちろんOK。

このユーザは OS 側のユーザとは一致しないので、hogehoge ユーザを使ってデータベースにアクセスするには pg_hba.conf を修正する。

local   all             all                                     peer

local   all             all                                     md5

以下のようにして反映

$ sudo systemctl restart postgresql.service

データベースを作る

同様に、postgres ユーザになってから操作します。

$ sudo -u postgres bash
$ psql 
postgres=# create database damedame;
CREATE DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 damedame  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)


8
5
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
8
5