0
0

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 5 years have passed since last update.

PostgreSQLメモ

Last updated at Posted at 2019-01-01

PostgreSQLについての学習

インストール

Windows

Windows Server2016+PostgreSQL9.6.11

「postgresql-9.6.11-1-windows-x64.exe」を実行したら以下のエラーが表示され、インストールできなかった。

image.png

とりあえず「–-install_runtimes 0」オプションを付けるとインストールができた。

Linux

1-1.インストールの準備

OSの種類、バージョンとPostgreSQLのバージョンを確認してダウンロードして

1-2.インストール(RHEL6)

yum install postgresql96-server

依存関係もある以下もインストールされる。
postgresql96
postgresql96-libs

1-3.アンインストール(RHEL6.9)

yum remove postgresql96-libs

依存関係があるので、postgresql-serverも削除される。

1-4.インストールされているものの確認

yum list installed | grep postgres

2.起動と停止

Linux

2-1.サービス起動

RHEL6(psql96)
初回のみinitdbが必要

service postgresql-9.6 initdb
service postgresql-9.6 start

initdbを行わないと以下のようなエラーになる。
image.png
initdbを行うと、「/var/lib/pgsql/9.6/data」にファイルが作成される。

管理者で接続

psql -U postgres
rootユーザのシェルから接続すると以下のエラーが発生する。
image.png
peer認証とは「Peer認証とは、カーネルからクライアント上のシステムユーザ名を取得し、PostgreSQLデータベースユーザと同一である場合のみ接続が許可される仕組み」で、パスワード入力が不要。
pg_hba.conf(/var/lib/pgsql/9.6/data/にある)を編集し、パスワード入力で接続できるようにする。

pg_hba.confファイルの編集

認証方式をpeerから変更する。
認証方式のドキュメント
https://www.postgresql.jp/document/9.6/html/auth-methods.html

データベースの作成

create database testdb;

ユーザの作成

create role testuser with login password 'testpassword';

ユーザで接続

psql -U testuser -d testdb

RHEL7

インストール

wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
rpm -ivh pgdg-redhat96-9.6-3.noarch.rpm
yum install -y postgresql96-server
/usr/pgsql-9.6/bin/postgresql96-setup initdb
systemctl start postgresql-9.6.service

テスト用のデータベースとユーザーの作成

su - postgres
psql
SQL
create database testdb;
create role testuser with login password 'testpassword';

テスト用ユーザーで、テスト用DBに接続

-bash-4.2$ psql -U testuser -d testdb
psql: FATAL:  ユーザ "testuser" で対向(peer)認証に失敗しました
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?