安装PostgreSQL在Ubuntu上

环境:Ubuntu 16.04.2 LTS。

安装PostgreSQL 9.5.7版本。

安装

% sudo apt-get update
% sudo apt-get install -y postgresql

设置

最初的设定

% sudo -i -u postgres
postgres% psql
postgres=# \du
List of roles
Role name |                         Attributes                         | Member of
----------+------------------------------------------------------------+-----------
postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \password postgres
Enter password for new role:
Enter it again:

创建用户

postgres% createuser -P hoge
postgres% psql
postgres=# \du
List of roles
Role name |                         Attributes                         | Member of
----------+------------------------------------------------------------+-----------
hoge      |                                                            | {}
postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# alter role hoge with password 'password';

创建数据库

postgres=# create database geho;
CREATE DATABASE
postgres=# \l
                              List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
geho      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
         |          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
         |          |          |             |             | postgres=CTc/postgres

在数据库中授予权限

postgres-# GRANT ALL ON DATABASE geho TO hoge;
postgres-# \l
                            List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
geho    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +
|          |          |             |             | postgres=CTc/postgres+
|          |          |             |             | ii=CTc/postgres
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
(4 rows)

普通用户对数据库的访问

hoge% psql postgres hoge
postgres=> \connect geho
geho=> CREATE TABLE fuga
geho-> (id int, name character varying(5));
CREATE TABLE
geho=> \dt
      List of relations
Schema | Name | Type  | Owner
--------+------+-------+-------
public | fuga | table | hoge
(1 row)
geho=> SELECT * FROM fuga;
id | name
----+------
(0 rows)

文献引用