ALTER ROLE

ALTER ROLEは、ロールの属性を変更するSQL文。

ALTER ROLE
CentOS で PostgreSQL を使ってみよう!(2) | Let's Postgres
ロールの属性を変更する(ALTER ROLE) | PostgreSQLの使い方

createuserは、

パート VI. リファレンス

ここによると、PostgreSQLクライアントアプリケーションの持つ「SQLのラッパーコマンド」らしい。
それに対して、属性変更では、psqlを使って「生SQL」のALTER ROLEを発行する必要がある模様。
testuser1は何の属性も付いてなかったので、DB作成可ぐらいにはしておく。

[postgres@localhost ~]$ psql
psql (11.7)
"help" でヘルプを表示します。

postgres=# \du
                                              ロール一覧
 ロール名  |                                    属性                                    | 所属グループ
-----------+----------------------------------------------------------------------------+--------------
 postgres  | スーパーユーザ, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
 testuser1 |                                                                            | {}

postgres=# ALTER ROLE testuser1 WITH CREATEDB;
ALTER ROLE
postgres=# \du
                                              ロール一覧
 ロール名  |                                    属性                                    | 所属グループ
-----------+----------------------------------------------------------------------------+--------------
 postgres  | スーパーユーザ, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
 testuser1 | DB作成可                                                                   | {}

postgres=#

SQLには';'(セミコロン)を付け忘れずに。
ALTER ROLEが返ってこないようだと失敗してる可能性が高い。
DB作成可が付加された。

createuser

createuserは、新しいユーザ(PostgreSQLにおけるロール)を作成する。

createuser
CentOS で PostgreSQL を使ってみよう!(2) | Let's Postgres

bg1@localhost ~]$ su
パスワード:
[root@localhost bg1]# service postgresql start
Redirecting to /bin/systemctl start postgresql.service
[root@localhost bg1]# su - postgres
[postgres@localhost ~]$ psql -l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |     アクセス権限      
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | 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
(3 行)

[postgres@localhost ~]$ createuser testuser1
[postgres@localhost ~]$ psql
psql (11.7)
"help" でヘルプを表示します。

postgres=# \du
                                              ロール一覧
 ロール名  |                                    属性                                    | 所属グループ
-----------+----------------------------------------------------------------------------+--------------
 postgres  | スーパーユーザ, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
 testuser1 |                                                                            | {}

postgres=#

ユーザを確認するのに、psqlで接続して、\duしてる。

作成済みのロール一覧を表示する | PostgreSQLの使い方

作成時に何も訊いてこない(昔はデフォルトで属性を訊いてきてた気が)せいか、testuser1に何の属性も付いてない・・・。

psql

PostgreSQLは、オープンソースRDBMS

日本PostgreSQLユーザ会 | 日本PostgreSQLユーザ会
yumでPostgreSQLをインストールしてみよう | Let's Postgres
CentOS で PostgreSQL を使ってみよう!(2) | Let's Postgres

ちょっとどころか随分古いけど上記の記事をみながら、psqlが使えるぐらいまでの準備。
まず、インストール。

[bg1@localhost ~]$ su
パスワード:
[root@localhost bg1]# yum install postgresql-server
メタデータの期限切れの最終確認: 3:17:41 時間前の 2020年03月18日 19時34分01秒 に実施しました。
依存関係が解決しました。
================================================================================
 Package                  Architecture  Version            Repository      Size
================================================================================
インストール:
 postgresql-server        x86_64        11.7-1.fc31        updates        5.2 M
依存関係のインストール:
 libpq                    x86_64        12.2-1.fc31        updates        212 k
 postgresql               x86_64        11.7-1.fc31        updates        1.5 M

トランザクションの概要
================================================================================
インストール  3 パッケージ

ダウンロードサイズの合計: 6.9 M
インストール済みのサイズ: 28 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/3): libpq-12.2-1.fc31.x86_64.rpm              57 kB/s | 212 kB     00:03    
(2/3): postgresql-11.7-1.fc31.x86_64.rpm        165 kB/s | 1.5 MB     00:09    
(3/3): postgresql-server-11.7-1.fc31.x86_64.rpm 159 kB/s | 5.2 MB     00:33    
--------------------------------------------------------------------------------
合計                                            207 kB/s | 6.9 MB     00:34    
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                        1/1
  インストール中   : libpq-12.2-1.fc31.x86_64                               1/3
  インストール中   : postgresql-11.7-1.fc31.x86_64                          2/3
  scriptletの実行中: postgresql-server-11.7-1.fc31.x86_64                   3/3
  インストール中   : postgresql-server-11.7-1.fc31.x86_64                   3/3
  scriptletの実行中: postgresql-server-11.7-1.fc31.x86_64                   3/3
  検証             : libpq-12.2-1.fc31.x86_64                               1/3
  検証             : postgresql-11.7-1.fc31.x86_64                          2/3
  検証             : postgresql-server-11.7-1.fc31.x86_64                   3/3

インストール済み:
  postgresql-server-11.7-1.fc31.x86_64         libpq-12.2-1.fc31.x86_64        
  postgresql-11.7-1.fc31.x86_64              

完了しました!
[root@localhost bg1]#

postgresユーザができてるか確認。

[root@localhost bg1]# less /etc/passwd
[root@localhost bg1]# id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
[root@localhost bg1]#

いた。
fingerでも、

[root@localhost bg1]# finger postgres
bash: finger: コマンドが見つかりませんでした...
コマンド finger' を提供するためにパッケージ 'finger' をインストールしますか? [N/y] y


 * キューで待機中...
 * パッケージの一覧をロード中。...
以下のパッケージはインストールされるべきものです:
 finger-0.17-65.fc31.x86_64 The finger client
変更したまま継続しますか? [N/y] y


 * キューで待機中...
 * 認証を待ち受け中...
 * キューで待機中...
 * パッケージをダウンロード中...
 * データを要求中...
 * 変更をテスト中...
 * パッケージのインストール中...
Login: postgres       Name: PostgreSQL Server
Directory: /var/lib/pgsql           Shell: /bin/bash
Never logged in.
No mail.
No Plan.

finger入ってなかったのかな。よくわからん。

[root@localhost bg1]# useradd testuser1
[root@localhost bg1]# passwd testuser1
ユーザー testuser1 のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。

postgresとは別にtestuser1を追加。

サービス起動に失敗
サービス起動に失敗

サービス起動に失敗。

【postgresql】起動しない | 夕湖津のブログ

initdbが先に必要らしい。

[root@localhost bg1]# postgresql-setup initdb
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@localhost bg1]#

これで、

今度は成功した
今度は成功した

今度は成功した。

[root@localhost bg1]# su - postgres
[postgres@localhost ~]$

postgresユーザに切り替え。

[postgres@localhost ~]$ psql -l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |     アクセス権限      
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | 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
(3 行)

[postgres@localhost ~]$

-lオプションでDB一覧表示。

[postgres@localhost ~]$ psql postgres
psql (11.7)
"help" でヘルプを表示します。

postgres=# \dt
リレーションが見つかりませんでした。
postgres=# exit
[postgres@localhost ~]$

postgresデータベース中身あらず。

[postgres@localhost ~]$ psql template0
psql: FATAL:  現在データベース"template0"は接続を受け付けません
[postgres@localhost ~]$ 

template0データベースは接続できず。

[postgres@localhost ~]$ psql template1
psql (11.7)
"help" でヘルプを表示します。

template1=# \dt
リレーションが見つかりませんでした。
template1=# exit
[postgres@localhost ~]$

template1データベース中身あらず。

AddHandler

実行するCGIの拡張子を増やしたい場合は、AddHandlerディレクティブに拡張子を追加する。

mod_mime - Apache HTTP サーバ バージョン 2.2

[root@localhost www]# ls
index.html  test.cgi
[root@localhost www]# vi test.cgi
[root@localhost www]# ls
index.html  test.cgi
[root@localhost www]# cp test.cgi test.pl
[root@localhost www]# vi test.pl
[root@localhost www]#

test.cgiをtest.plという名前でコピーして、

#!/usr/bin/perl

# ヒアドキュメントでテスト用CGIを書く.
$text = <<END; # ENDの前まで格納する.
Content-Type: text/html

<html>
  <head>
    <title>
      TestPerl
    </title>
  </head>
  <body>
    TestPerl
  </body>
</html>
END

# $textの出力.
print $text; # $textを出力.

Perlスクリプトとして保存しておく。
これでtest.plにアクセスすると、

ダウンロードしようとする
ダウンロードしようとする

ダウンロードしようとする。
これをCGIとして実行したい。

Alias /local "/usr/local/www"
<Directory "/usr/local/www">
    Options ExecCGI
    AddHandler cgi-script .cgi .pl
    Require all granted
</Directory>

AddHandlerに.plを追加すると、

.plでもCGIとして実行される
.plでもCGIとして実行される

.plでもCGIとして実行される。

Options(ExecCGI)

Optionsは、指定のディレクトリに、特定の機能を付与するディレクティブ。

core - Apache HTTP サーバ バージョン 2.4
特定のディレクトリでCGIを実行する(Option, AddHandler) - CGIの利用 - Apache入門

基本的には、ScriptAliasで指定したディレクトリはCGIを実行できる。
ただし、「そうでない」ディレクトリでも「.cgiの拡張子など」であればCGIを実行したい場合もあるだろう。
OptionsでExecCGIを指定し、AddHandlercgi-scriptの実行拡張子として".cgi"などを追加すれば、CGIを実行できる。
(「」を付けたところをこれまで勘違いしてて、ScriptAliasと一緒に使うわけじゃない。あくまでそうでないディレクトリに対して。)

Alias /local "/usr/local/www"
<Directory "/usr/local/www">
    Require all granted
</Directory>

Alias(ScriptAliasではない)で/localを指定する。
/usr/local/wwwの下にtest.cgiを作成し、

#!/usr/bin/perl

# ヒアドキュメントでテスト用CGIを書く.
$text = <<END; # ENDの前まで格納する.
Content-Type: text/html

<html>
  <head>
    <title>
      TestCGI2
    </title>
  </head>
  <body>
    TestCGI2
  </body>
</html>
END

# $textの出力.
print $text; # $textを出力.

こう書く。

ScriptAliasではないのでテキストとして表示
ScriptAliasではないのでテキストとして表示

当然、ScriptAliasではないのでテキストとして表示されてしまう。

Alias /local "/usr/local/www"
<Directory "/usr/local/www">
    Options ExecCGI
    AddHandler cgi-script .cgi
    Require all granted
</Directory>

Options ExecCGIとAddHandler cgi-scriptで.cgiを指定。

これでCGIとして表示される。
これでCGIとして表示される。

これでScriptAliasでなくても、.cgiであればCGIとして表示される。

ScriptAlias

指定のURLを、CGIの実行ディレクトリにマッピングする場合は、ScriptAliasディレクティブを使う。

mod_alias - Apache HTTP サーバ バージョン 2.4
Fedora 31 : Apache httpd : CGI スクリプトを利用する : Server World

[bg1@localhost ~]$ su
パスワード:
[root@localhost bg1]# cd /etc/httpd/conf
[root@localhost conf]# vi httpd.conf

httpd.confを開くと、前回の<IfModule alias_module>の中に、

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

と既に書いてあるので、/cgi-bin/の下のCGIファイルにアクセスすると、/var/www/cgi-bin/の下の指定のCGIが実行される。

[root@localhost conf]# cd /var/www/cgi-bin/
[root@localhost cgi-bin]# ls
[root@localhost cgi-bin]# vi testcgi1.cgi
[root@localhost cgi-bin]# which perl
/usr/bin/perl
[root@localhost cgi-bin]# vi testcgi1.cgi

Perlでテスト用CGIを作ってみる。

#!/usr/bin/perl

# ヒアドキュメントでテスト用CGIを書く.
$text = <<END; # ENDの前まで格納する.
Content-Type: text/html

<html>
  <head>
    <title>
      TestCGI1
    </title>
  </head>
  <body>
    TestCGI1
  </body>
</html>
END

# $textの出力.
print $text; # $textを出力.

せっかくなので、ヒアドキュメントで作成。

[root@localhost cgi-bin]# ls -al
合計 12
drwxr-xr-x. 2 root root 4096  3月 18 12:13 .
drwxr-xr-x. 5 root root 4096  3月 16 13:07 ..
-rw-r--r--. 1 root root  304  3月 18 12:13 testcgi1.cgi
[root@localhost cgi-bin]# chmod 755 testcgi1.cgi
[root@localhost cgi-bin]# ls -al
合計 12
drwxr-xr-x. 2 root root 4096  3月 18 12:13 .
drwxr-xr-x. 5 root root 4096  3月 16 13:07 ..
-rwxr-xr-x. 1 root root  304  3月 18 12:13 testcgi1.cgi
[root@localhost cgi-bin]# apachectl start

chmodで実行権限を付与して、Apacheを起動すると、

テスト用CGIが実行された
テスト用CGIが実行された

テスト用CGIが実行された。

Alias

Aliasは、指定のURLを実際のファイルパスにマップするディレクティブ。

mod_alias - Apache HTTP サーバ バージョン 2.4

これまでも、"/htmltest"なら"/var/www/htmltest"にマップしていたけど、本来は、"/"なら"/var/www/html"の下なので、"/htmltest"なら"/var/www/html/htmltest"となるはずだが、Aliasによって、違う場所"/var/www/htmltest"にマップできるわけである。

さて、これまでDocumentRootの下でこういうことをしてきたけど、全く違う場所にマップできるかというと、それも可能。
ただし、ちょっとハマったので注意。

最初は、

Alias /htmltest /var/www/htmltest/
<Directory "/var/www/htmltest/">
    Order deny,allow
    Deny from all
    Allow from localhost
</Directory>

<Directory "/var/www/htmltest/sub_dir1">
    Order allow,deny
    Allow from all
    Deny from localhost
</Directory>

Alias /www2 /var/www2/html
<Directory "/var/www2/html">
    Order allow,deny
    Allow from all
</Directory>

/varの下にwww2というディレクトリを作り、これまでのAliasと同様にこのように定義した。
ところが、これだと403のForbeddenになってしまう。
ディレクトリやindex.htmlのパーミッション、オーナー、グループなど確認したが、どうしても解決できない。

次に、

Alias /home /home/bg1/www
<Directory "/home/bg1/www">
    Require all granted
</Directory>

ホームディレクトリを公開する方法があったので、これも試したが同様に403のForbedden。

Alias で別 DocumentRoot を作り CGI, SSI, html を動かす

ただ、どうもApache2.4では、Allow、Denyを使う方法ではなく、Requireを使う方法じゃないと、ダメだというのはわかった。

Apache - apacheでDocumentRoot以外のアクセス方法について|teratail

結論として、/usr/localの下にディレクトリを作り、そこへのAliasとRequire all grantedを<IfModule alias_module>タグの中で定義すると、うまくいった。

[root@localhost conf]# cd /usr/local/www/
[root@localhost www]# ls
index.html
[root@localhost www]# ls -al
合計 12
drwxrwxr-x.  2 apache apache 4096  3月 17 16:41 .
drwxr-xr-x. 13 root   root   4096  3月 17 16:39 ..
-rwxrwxr-x.  1 apache apache   96  3月 17 16:41 index.html
[root@localhost www]# cd ..
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src  www
[root@localhost local]# ls -al
合計 52
drwxr-xr-x. 13 root   root   4096  3月 17 16:39 .
drwxr-xr-x. 12 root   root   4096 10月 24 08:12 ..
drwxr-xr-x.  2 root   root   4096  7月 25  2019 bin
drwxr-xr-x.  2 root   root   4096  7月 25  2019 etc
drwxr-xr-x.  2 root   root   4096  7月 25  2019 games
drwxr-xr-x.  2 root   root   4096  7月 25  2019 include
drwxr-xr-x.  2 root   root   4096  7月 25  2019 lib
drwxr-xr-x.  2 root   root   4096  7月 25  2019 lib64
drwxr-xr-x.  2 root   root   4096  7月 25  2019 libexec
drwxr-xr-x.  2 root   root   4096  7月 25  2019 sbin
drwxr-xr-x.  5 root   root   4096 10月 24 08:12 share
drwxr-xr-x.  2 root   root   4096  7月 25  2019 src
drwxrwxr-x.  2 apache apache 4096  3月 17 16:41 www
[root@localhost local]#

一応、apacheオーナーとグループにしたほうがいいらしいのでそうしてる。
パーミッションも775だけど、これもどこまで締めたり開けたりするかはわからない。
とにかく/usr/localにwwwフォルダを置いて、index.htmlは、

<html>
  <head>
    <title>UserLocal</title>
  </head>
  <body>
    UserLocal
  </body>
</html>

UserLocal。
httpd.confは、

<IfModule alias_module>
    #
    # Redirect: Allows you to tell clients about documents that used to
    # exist in your server's namespace, but do not anymore. The client
    # will make a new request for the document at its new location.
    # Example:
    # Redirect permanent /foo http://www.example.com/bar

    #
    # Alias: Maps web paths into filesystem paths and is used to
    # access content that does not live under the DocumentRoot.
    # Example:
    # Alias /webpath /full/filesystem/path
    #
    # If you include a trailing / on /webpath then the server will
    # require it to be present in the URL.  You will also likely
    # need to provide a <Directory> section to allow access to
    # the filesystem path.

    Alias /local "/usr/local/www"
    <Directory "/usr/local/www">
        Require all granted
    </Directory>

    #
    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

これまでIfModuleの中で定義してなかったけど、さらっとここに書けと書いてあったので。
外に書いた場合に上手くいくかはわかない。
これで、

アクセス出来た
アクセス出来た

/localで/usr/local/wwwの下にアクセス出来た。