CREATE TABLE

CREATE TABLEは、新しいテーブルを作成するSQL文。

CREATE TABLE
2.3. 新しいテーブルの作成

testuser1dbに、新しいテーブルuser_profileを作成する。

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

testuser1db=> CREATE TABLE user_profile ( name varchar(32) , address varchar(256) , age int );
CREATE TABLE
testuser1db=> \dt
                リレーション一覧
 スキーマ |     名前     |    型    |  所有者  
----------+--------------+----------+-----------
 public   | user_profile | テーブル | testuser1
(1 行)

testuser1db=>

作成できた。
\dtでテーブル一覧が見れる。
最初、"user"で作ろうとしたら、エラーが出た。
予約されてんのかな・・・。

createdb

createdbは、新しいデータベースを作成する。

createdb

こちらは、またPostgreSQLクライアントアプリケーションのラッパーコマンド。

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

ここにあるように、postgresユーザから、testuser1名義でデータベースを作ってみる。

[postgres@localhost ~]$ createdb testuser1db -U testuser1
createdb: データベース template1 に接続できませんでした: FATAL:  ユーザ "testuser1" で対向(peer)認証に失敗しました
[postgres@localhost ~]$

"FATAL: Ident authentication failed"とは違う気がするが、とにかく失敗する。
Linuxユーザのtestuser1になってから、もう一度実行。

[postgres@localhost ~]$ exit
ログアウト
[root@localhost bg1]# su - testuser1
[testuser1@localhost ~]$ createdb testuser1db
[testuser1@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
 testuser1db | testuser1 | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       |
(4 行)

[testuser1@localhost ~]$

今度は作成できた。

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として表示される。