0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ProxmoxのLXCコンテナにPostgreSQLのサーバーを建てるまで

Posted at

筆者について

name → Koki Teramoto
age → 18
language → ja_JP, en_US

本編

題名の通りなのですが、少してこずったので備忘録程度に書いておきます。参考記事は以下の2つです。ありがとうございます。

コンテナの情報は以下のとおりです。

image.png

PostgreSQLのインストール

インストールはとても簡単です。

sudo apt install postgresql postgresql-contrib

アクセス許可の設定

ローカル以外からのアクセスを有効化する必要がありますのでそちらの設定を加えていきます。ちなみに、以下の設定ファイルは/etc/postgresql/{version}/mainにあります。

postgresql.confの設定

リッスンポートを変更します。以下のように書き換えてください。

postgresql.conf
listen_addresses = '*'

pg_hba.confの設定

ここは気をつけましょう。pg_hba.confの下の方まで行きます。きっとこんな記述箇所があるはずです。

pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

変更するのはここのIPv4のところです。CIDER表記でアクセスできるIPを変更してください。また、METHODmd5に変更しましょう。僕は、以下のように設定を変更しました。

pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             192.168.0.0/16          md5
host    all             all             100.0.0.0/8             md5
# IPv6 local connections:
#host    all             all             ::1/128                scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

IPv6を設定しようかと思いましたがめんどくさかったので、とりあえずオフにしてIPv4の設定だけにしました。そして、TailscaleとLANからのアクセスを許可するためにそれぞれ設定を変更しています。

ユーザー(ロール)の作成

まずはデフォルトユーザーでログインします。

sudo -u postgres psql

次に、ユーザーを作成します。

CREATE USER username WITH PASSWORD 'password';

データベースを作成します。

CREATE DATABASE database_name;

ローカルの検証用なので、スーパーユーザー権限を付与しちゃいます。ここは適宜必要に応じて必ず変更してください。

ALTER ROLE username WITH SUPERUSER

あとは、ログインするだけです!試してみましょう。

psql -h 192.168.101.100 -U test -d test_db

見事にログインできました!

最後に

これ、Javaの勉強用に作ったSQLサーバー、適用にパーって作ったんですけども、Javaの方が難しくて驚いてます。データベースの勉強も進めていかねば、、、。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?