LoginSignup
4
4

More than 5 years have passed since last update.

Ubuntu14.10にPostgreSQLをインストールする

Last updated at Posted at 2015-09-26

したいこと

Ubuntu14.10にPostgreSQLをインストールしデータベースを構築する

手順

インストール

$ sudo apt-get install -y postgresql-9.4

起動

$ sudo /etc/init.d/postgresql start
 * Starting PostgreSQL 9.4 database server              [ OK ]

接続

データベースにUnixログインユーザ USER がロールとして存在しないので接続できない

$ psql
psql: FATAL:  role "USER" does not exist

ロール作成

ユーザ postgres として接続

$ su - postgres
$ psql
psql (9.4.4)
Type "help" for help.

postgres=#

ロール確認

postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}

ロール sample_role 作成

postgres=# CREATE ROLE sample_role WITH LOGIN CREATEDB PASSWORD 'sample_role';

ロール sample_role が作成されたのを確認

postgres=# \du
                              List of roles
  Role name  |                   Attributes                   | Member of 
-------------+------------------------------------------------+-----------
 postgres    | Superuser, Create role, Create DB, Replication | {}
 sample_role | Create DB                                      | {}

データベース sample 作成

postgres=# CREATE DATABASE sample OWNER sample_role;
CREATE DATABASE

データベース sample が作成されたのを確認

postgres=# \l
                                   List of databases
   Name    |    Owner    | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+-------------+----------+-------------+-------------+-----------------------
 postgres  | postgres    | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 sample    | sample_role | 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)

クライアント認証の設定

現状ではpeer認証となっていて、ロールとUnixユーザ名が一致していないとログインできない状態となっているので1

$ psql -U sample_role sample
psql: FATAL:  Peer authentication failed for user "sample_role"
/etc/postgresql/9.4/main/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     peer

peer認証をmd5認証に変更し、

/etc/postgresql/9.4/main/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     md5

再起動

$ sudo /etc/init.d/postgresql restart
 * Restarting PostgreSQL 9.4 database server              [ OK ]

sample_role でログインできることを確認

$ psql -U sample_role sample
Password for user sample_role: 
psql (9.4.4)
Type "help" for help.

sample=> 
4
4
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
4
4