Chiroru's Diary

日々の学びをちょこちょこメモしていきます

Linux(Debian10.3)にPostgreSQLをインストールする

【学習内容】

【目次】

はじめに

データベースの学習に伴いLinux(Debian)にPostgreSQLインストールをしたので、主にその手順のまとめと、ユーザーの追加方法を書いておきます。

環境

環境は以下の通り。

$ cat /etc/debian_version
10.3

$ psql --version
psql (PostgreSQL) 11.7

こちらLinux downloads (Debian) を参考にインストールを進めた。

【インストール手順 】

1.リポジトリ追加、パスを記載

/etc/apt/sources.list.d/pgdg.list のファイルを作成後、以下のリポジトリのパスを記載する。

# echo  deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main  > /etc/apt/sources.list.d/pgdg.list

2. CA証明書インストール

CA証明書(ルート証明書)を取得する。
今回は同時にwgetコマンドもインストールしている。

$ sudo apt install wget ca-certificates

3. PostgreSQL公開鍵を追加

PostgreSQLの公開鍵をインポートし、updateする。

$ sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

$ sudo apt-get update

4. PostgreSQLをインストール

DebianにはデフォルトでPostgreSQLが含まれているため、apt-getでインストールする。

$ sudo apt-get install postgresql-11

$ psql --version
psql (PostgreSQL) 11.7 (Debian 11.7-0+deb10u1)

PostgreSQLのインストールと同時にpostgresユーザー(データベースの管理ユーザー)が作成される。さっそくこのユーザーでログインする。

$ su - postgres
postgres@:~$

ユーザーの追加

次に新規ユーザーとPWの登録をする。

$ createuser --pwprompt --interactive <ユーザー名>

Enter password for new role: 
Enter it again: 
Shall the new role be a superuser? (y/n) → y

作成したユーザーでログイン(データーベース接続)してみる。

$ psql -U <ユーザー名> -d postgres -h localhost

※ $ psql -d database -U user -h host
-d: データベース名(未指定だと、ログインユーザー名のデータベースに接続)
-U: ユーザ名(未指定だと、ログインユーザー名)
-h: ホスト名(未指定だと、localhost)

以下が表示されればOK

postgres=#

ユーザー一覧の確認は以下。

postgres=# \du

終了の際は以下。

-- psqlの終了
postgres=# \q

参考