documents:quick:quick-0018
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン | |||
| documents:quick:quick-0018 [2024/11/01 16:25] – ↷ 移動操作に合わせてリンクを書き換えました。 k896951 | documents:quick:quick-0018 [2026/05/07 22:20] (現在) – ↷ 移動操作に合わせてリンクを書き換えました。 k896951 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== FreeBSDのパッケージでPostgreSQLをクイックセットアップ ====== | ||
| + | |||
| + | 2017/ | ||
| + | FreeBSDのpkgコマンドでお手軽にPostgreSQLをインストール、セットアップする。 | ||
| + | portsでソースからコンパイルするより速いし、ちゃちゃっと準備するにはおすすめ。 | ||
| + | |||
| + | 今回は ezjail でJailをこしらえてその中にインストールします。< | ||
| + | |||
| + | ===== PostgreSQLインストール前の準備 ===== | ||
| + | |||
| + | Jailにインストールする場合、事前に設定しておく内容。\\ | ||
| + | 既にJailインスタンス pg が作成され起動しているものとします。 | ||
| + | |||
| + | Jailの通信許可と共有メモリ利用許可を出します。 | ||
| + | |||
| + | [[documents: | ||
| + | |||
| + | ===== PostgreSQLインストール ===== | ||
| + | |||
| + | Jailにインストールする場合はJailにログインしておきます。 | ||
| + | |||
| + | ==== PostgreSQLのパッケージ名確認 ==== | ||
| + | |||
| + | pkgコマンドの searchサブコマンドで postgres を検索します。 | ||
| + | |||
| + | < | ||
| + | root@pg:~ # pkg search postgres | ||
| + | exim-postgresql-4.88 | ||
| + | fpc-postgres-3.0.0_1 | ||
| + | libgda4-postgresql-4.2.12_2 | ||
| + | libgda5-postgresql-5.2.4 | ||
| + | luasql-postgres-2.3.3 | ||
| + | mysqlphp2postgres-0.95 | ||
| + | nagios-check_postgres-2.22.0 | ||
| + | nagios-check_postgres_replication-2010.12.16 Nagios plugin to check the lag between a postgresql replica and master | ||
| + | opensmtpd-extras-table-postgresql-201606230001 PostgreSQL table support for OpenSMTPD | ||
| + | p5-PostgreSQL-PLPerl-Call-1.006_1 Simple interface for calling SQL functions from PostgreSQL PL/Perl | ||
| + | p5-PostgreSQL-PLPerl-Trace-1.001_1 Simple way to trace execution of Perl statements in PL/Perl | ||
| + | p5-Test-postgresql-0.09_1 | ||
| + | pgtcl-postgresql92-2.0.0_1 | ||
| + | pgtcl-postgresql93-2.0.0_1 | ||
| + | pgtcl-postgresql94-2.0.0_1 | ||
| + | pgtcl-postgresql95-2.0.0_1 | ||
| + | pgtcl-postgresql96-2.0.0_1 | ||
| + | postgresql-jdbc-9.2.1004 | ||
| + | postgresql-libpgeasy-3.0.4_1 | ||
| + | postgresql-libpqxx-4.0.1_1 | ||
| + | postgresql-libpqxx3-3.1.1_1 | ||
| + | postgresql-odbc-09.05.0400 | ||
| + | postgresql-plproxy-2.5_1 | ||
| + | postgresql-relay-1.3.2_1 | ||
| + | postgresql-repmgr-3.1.5 | ||
| + | postgresql-repmgr2-2.0.3_1 | ||
| + | postgresql92-client-9.2.19_1 | ||
| + | postgresql92-contrib-9.2.19_1 | ||
| + | postgresql92-docs-9.2.19_1 | ||
| + | postgresql92-plperl-9.2.19_1 | ||
| + | postgresql92-plpython-9.2.19_1 Module for using Python to write SQL functions | ||
| + | postgresql92-pltcl-9.2.19_1 | ||
| + | postgresql92-server-9.2.19_1 | ||
| + | postgresql93-client-9.3.15_1 | ||
| + | postgresql93-contrib-9.3.15_1 | ||
| + | postgresql93-docs-9.3.15_1 | ||
| + | postgresql93-plperl-9.3.15_1 | ||
| + | postgresql93-plpython-9.3.15_1 Module for using Python to write SQL functions | ||
| + | postgresql93-pltcl-9.3.15_1 | ||
| + | postgresql93-plv8js-1.4.8 | ||
| + | postgresql93-server-9.3.15_1 | ||
| + | postgresql94-client-9.4.10_1 | ||
| + | postgresql94-contrib-9.4.10_1 | ||
| + | postgresql94-docs-9.4.10_1 | ||
| + | postgresql94-plperl-9.4.10_1 | ||
| + | postgresql94-plpython-9.4.10_1 Module for using Python to write SQL functions | ||
| + | postgresql94-pltcl-9.4.10_1 | ||
| + | postgresql94-server-9.4.10_1 | ||
| + | postgresql95-client-9.5.5_1 | ||
| + | postgresql95-contrib-9.5.5 | ||
| + | postgresql95-docs-9.5.5_1 | ||
| + | postgresql95-plperl-9.5.5_1 | ||
| + | postgresql95-plpython-9.5.5_1 | ||
| + | postgresql95-pltcl-9.5.5_1 | ||
| + | postgresql95-server-9.5.5_1 | ||
| + | postgresql96-client-9.6.1 | ||
| + | postgresql96-contrib-9.6.1 | ||
| + | postgresql96-docs-9.6.1_1 | ||
| + | postgresql96-plperl-9.6.1_1 | ||
| + | postgresql96-plpython-9.6.1_1 | ||
| + | postgresql96-pltcl-9.6.1_1 | ||
| + | postgresql96-server-9.6.1_1 | ||
| + | proftpd-mod_sql_postgres-1.3.5b PostgreSQL ProFTPD module | ||
| + | py35-postgresql-1.1.0_3 | ||
| + | rubygem-dm-postgres-adapter-1.2.0 PostgreSQL Adapter for DataMapper | ||
| + | rubygem-do_postgres-0.10.13 | ||
| + | rubygem-postgres_ext-3.0.0 | ||
| + | root@pg: | ||
| + | </ | ||
| + | |||
| + | ==== 指定パッケージをインストール ==== | ||
| + | |||
| + | 古いバージョンを使う理由がないならこのリストで最新の postgresql96-server-9.6.1_1 をインストールしましょう。 | ||
| + | 依存関係のあるパッケージも同時にインストールされます。 | ||
| + | |||
| + | < | ||
| + | root@pg:~ # pkg install postgresql96-server-9.6.1_1 | ||
| + | Updating FreeBSD repository catalogue... | ||
| + | FreeBSD repository is up-to-date. | ||
| + | All repositories are up-to-date. | ||
| + | The following 8 package(s) will be affected (of 0 checked): | ||
| + | |||
| + | New packages to be INSTALLED: | ||
| + | postgresql96-server: | ||
| + | libxml2: 2.9.4 | ||
| + | icu: 58.2,1 | ||
| + | gettext-runtime: | ||
| + | indexinfo: 0.2.6 | ||
| + | postgresql96-client: | ||
| + | perl5: 5.24.1.r4_1 | ||
| + | readline: 6.3.8 | ||
| + | |||
| + | Number of packages to be installed: 8 | ||
| + | |||
| + | The process will require 130 MiB more space. | ||
| + | 30 MiB to be downloaded. | ||
| + | |||
| + | Proceed with this action? [y/N]: y | ||
| + | [pg] Fetching postgresql96-server-9.6.1_1.txz: | ||
| + | [pg] Fetching libxml2-2.9.4.txz: | ||
| + | [pg] Fetching icu-58.2, | ||
| + | [pg] Fetching gettext-runtime-0.19.8.1_1.txz: | ||
| + | [pg] Fetching indexinfo-0.2.6.txz: | ||
| + | [pg] Fetching postgresql96-client-9.6.1.txz: | ||
| + | [pg] Fetching perl5-5.24.1.r4_1.txz: | ||
| + | [pg] Fetching readline-6.3.8.txz: | ||
| + | Checking integrity... done (0 conflicting) | ||
| + | [pg] [1/8] Installing indexinfo-0.2.6... | ||
| + | [pg] [1/8] Extracting indexinfo-0.2.6: | ||
| + | [pg] [2/8] Installing gettext-runtime-0.19.8.1_1... | ||
| + | [pg] [2/8] Extracting gettext-runtime-0.19.8.1_1: | ||
| + | [pg] [3/8] Installing perl5-5.24.1.r4_1... | ||
| + | [pg] [3/8] Extracting perl5-5.24.1.r4_1: | ||
| + | [pg] [4/8] Installing readline-6.3.8... | ||
| + | [pg] [4/8] Extracting readline-6.3.8: | ||
| + | [pg] [5/8] Installing libxml2-2.9.4... | ||
| + | [pg] [5/8] Extracting libxml2-2.9.4: | ||
| + | [pg] [6/8] Installing icu-58.2, | ||
| + | [pg] [6/8] Extracting icu-58.2,1: 100% | ||
| + | [pg] [7/8] Installing postgresql96-client-9.6.1... | ||
| + | [pg] [7/8] Extracting postgresql96-client-9.6.1: | ||
| + | [pg] [8/8] Installing postgresql96-server-9.6.1_1... | ||
| + | ===> Creating groups. | ||
| + | Creating group ' | ||
| + | ===> Creating users | ||
| + | Creating user ' | ||
| + | |||
| + | =========== BACKUP YOUR DATA! ============= | ||
| + | As always, backup your data before | ||
| + | upgrading. If the upgrade leads to a higher | ||
| + | minor revision (e.g. 8.3.x -> 8.4), a dump | ||
| + | and restore of all databases is | ||
| + | required. This is *NOT* done by the port! | ||
| + | =========================================== | ||
| + | [pg] [8/8] Extracting postgresql96-server-9.6.1_1: | ||
| + | Message from perl5-5.24.1.r4_1: | ||
| + | The / | ||
| + | For shebangs, you should either use: | ||
| + | |||
| + | # | ||
| + | |||
| + | or | ||
| + | |||
| + | # | ||
| + | |||
| + | The first one will only work if you have a / | ||
| + | the second will work as long as perl is in PATH. | ||
| + | Message from postgresql96-client-9.6.1: | ||
| + | The PostgreSQL port has a collection of "side orders": | ||
| + | |||
| + | postgresql-docs | ||
| + | For all of the html documentation | ||
| + | |||
| + | p5-Pg | ||
| + | A perl5 API for client access to PostgreSQL databases. | ||
| + | |||
| + | postgresql-tcltk | ||
| + | If you want tcl/tk client support. | ||
| + | |||
| + | postgresql-jdbc | ||
| + | For Java JDBC support. | ||
| + | |||
| + | postgresql-odbc | ||
| + | For client access from unix applications using ODBC as access | ||
| + | method. Not needed to access unix PostgreSQL servers from Win32 | ||
| + | using ODBC. See below. | ||
| + | |||
| + | ruby-postgres, | ||
| + | For client access to PostgreSQL databases using the ruby & python | ||
| + | languages. | ||
| + | |||
| + | postgresql-plperl, | ||
| + | For using perl5, tcl & ruby as procedural languages. | ||
| + | |||
| + | postgresql-contrib | ||
| + | Lots of contributed utilities, postgresql functions and | ||
| + | datatypes. There you find pg_standby, pgcrypto and many other cool | ||
| + | things. | ||
| + | |||
| + | etc... | ||
| + | Message from postgresql96-server-9.6.1_1: | ||
| + | For procedural languages and postgresql functions, please note that | ||
| + | you might have to update them when updating the server. | ||
| + | |||
| + | If you have many tables and many clients running, consider raising | ||
| + | kern.maxfiles using sysctl(8), or reconfigure your kernel | ||
| + | appropriately. | ||
| + | |||
| + | The port is set up to use autovacuum for new databases, but you might | ||
| + | also want to vacuum and perhaps backup your database regularly. There | ||
| + | is a periodic script, / | ||
| + | you may find useful. You can use it to backup and perform vacuum on all | ||
| + | databases nightly. Per default, it performs `vacuum analyze' | ||
| + | script for instructions. For autovacuum settings, please review | ||
| + | ~pgsql/ | ||
| + | |||
| + | If you plan to access your PostgreSQL server using ODBC, please | ||
| + | consider running the SQL script / | ||
| + | to get the functions required for ODBC compliance. | ||
| + | |||
| + | Please note that if you use the rc script, | ||
| + | / | ||
| + | (UTF-8) will be used to store character data by default. | ||
| + | postgresql_initdb_flags or use login.conf settings described below to | ||
| + | alter this behaviour. See the start rc script for more info. | ||
| + | |||
| + | To set limits, environment stuff like locale and collation and other | ||
| + | things, you can set up a class in / | ||
| + | the database. Add something similar to this to / | ||
| + | --- | ||
| + | postgres:\ | ||
| + | : | ||
| + | : | ||
| + | : | ||
| + | --- | ||
| + | and run `cap_mkdb / | ||
| + | Then add ' | ||
| + | |||
| + | ====================================================================== | ||
| + | |||
| + | To initialize the database, run | ||
| + | |||
| + | / | ||
| + | |||
| + | You can then start PostgreSQL by running: | ||
| + | |||
| + | / | ||
| + | |||
| + | For postmaster settings, see ~pgsql/ | ||
| + | |||
| + | NB. FreeBSD' | ||
| + | See ~pgsql/ | ||
| + | |||
| + | ====================================================================== | ||
| + | |||
| + | To run PostgreSQL at startup, add | ||
| + | ' | ||
| + | root@pg:~ # | ||
| + | </ | ||
| + | |||
| + | ユーザ postgres が作成されています。PostgreSQLの設定ファイルの修正等はこのユーザにログインして実施します。 | ||
| + | |||
| + | ==== / | ||
| + | |||
| + | 指示に従い / | ||
| + | |||
| + | <code txt / | ||
| + | postgresql_enable=" | ||
| + | </ | ||
| + | |||
| + | ==== データベース初期化 ==== | ||
| + | |||
| + | PostgreSQLのインストール後の説明は一部古い部分があるので適宜置き換えして実行してください。もちろん、説明のまま実行しても問題はありませんが。 | ||
| + | |||
| + | 以下は説明中にある | ||
| + | < | ||
| + | / | ||
| + | </ | ||
| + | に対応するものです。 | ||
| + | |||
| + | < | ||
| + | root@pg:~ # service postgresql initdb | ||
| + | The files belonging to this database system will be owned by user " | ||
| + | This user must also own the server process. | ||
| + | |||
| + | The database cluster will be initialized with locale " | ||
| + | The default text search configuration will be set to " | ||
| + | |||
| + | Data page checksums are disabled. | ||
| + | |||
| + | creating directory / | ||
| + | creating subdirectories ... ok | ||
| + | selecting default max_connections ... 100 | ||
| + | selecting default shared_buffers ... 128MB | ||
| + | selecting dynamic shared memory implementation ... posix | ||
| + | creating configuration files ... ok | ||
| + | running bootstrap script ... ok | ||
| + | performing post-bootstrap initialization ... ok | ||
| + | syncing data to disk ... ok | ||
| + | |||
| + | WARNING: enabling " | ||
| + | You can change this by editing pg_hba.conf or using the option -A, or | ||
| + | --auth-local and --auth-host, | ||
| + | |||
| + | Success. You can now start the database server using: | ||
| + | |||
| + | / | ||
| + | |||
| + | root@pg:~ # | ||
| + | </ | ||
| + | / | ||
| + | |||
| + | |||
| + | なお、共有メモリの利用許可がないとこんなエラーが出ます。 | ||
| + | < | ||
| + | root@pg:/ | ||
| + | The files belonging to this database system will be owned by user " | ||
| + | This user must also own the server process. | ||
| + | |||
| + | The database cluster will be initialized with locale " | ||
| + | The default text search configuration will be set to " | ||
| + | |||
| + | Data page checksums are disabled. | ||
| + | |||
| + | creating directory / | ||
| + | creating subdirectories ... ok | ||
| + | selecting default max_connections ... 10 | ||
| + | selecting default shared_buffers ... 400kB | ||
| + | selecting dynamic shared memory implementation ... posix | ||
| + | creating configuration files ... ok | ||
| + | running bootstrap script ... FATAL: | ||
| + | DETAIL: | ||
| + | child process exited with exit code 1 | ||
| + | initdb: removing data directory "/ | ||
| + | root@pg:/ | ||
| + | </ | ||
| + | |||
| + | ==== pg_hba.conf の編集 ==== | ||
| + | |||
| + | デフォルトでは外部からの接続ができないので pg_hba.conf を編集して接続許可を出します。 | ||
| + | |||
| + | < | ||
| + | root@pg:/ | ||
| + | total 120 | ||
| + | -rw------- | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | -rw------- | ||
| + | -rw------- | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | drwx------ | ||
| + | -rw------- | ||
| + | -rw------- | ||
| + | -rw------- | ||
| + | -rw------- | ||
| + | root@pg:/ | ||
| + | $ vi pg_hba.conf | ||
| + | </ | ||
| + | |||
| + | 以下の2行を追加してください。 | ||
| + | <code txt / | ||
| + | host all | ||
| + | host all | ||
| + | </ | ||
| + | 最初の行はJailに割り当てたIPアドレスそのもの。\\ | ||
| + | 次の行はPostgreSQLに接続可能なIPアドレス範囲です。各環境に合わせて変更してください。 | ||
| + | |||
| + | |||
| + | ===== PostgreSQLの起動 ===== | ||
| + | |||
| + | 以下は説明中にある | ||
| + | < | ||
| + | / | ||
| + | </ | ||
| + | に対応するものです。 | ||
| + | |||
| + | < | ||
| + | $ ^D | ||
| + | root@pg:~ # service postgresql start | ||
| + | LOG: could not create IPv6 socket: Protocol not supported | ||
| + | LOG: ending log output to stderr | ||
| + | HINT: Future log output will go to log destination " | ||
| + | root@pg:~ # | ||
| + | </ | ||
| + | |||
| + | ===== PostgreSQLにユーザとデータベースを定義する ===== | ||
| + | |||
| + | 接続に使うユーザ kamo と、データベース kamonohashi を作成してみます。 | ||
| + | |||
| + | < | ||
| + | root@pg:~ # su postgres | ||
| + | $ createuser --help | ||
| + | createuser creates a new PostgreSQL role. | ||
| + | |||
| + | Usage: | ||
| + | createuser [OPTION]... [ROLENAME] | ||
| + | |||
| + | Options: | ||
| + | -c, --connection-limit=N | ||
| + | -d, --createdb | ||
| + | -D, --no-createdb | ||
| + | -e, --echo | ||
| + | -E, --encrypted | ||
| + | -g, --role=ROLE | ||
| + | -i, --inherit | ||
| + | member of (default) | ||
| + | -I, --no-inherit | ||
| + | -l, --login | ||
| + | -L, --no-login | ||
| + | -N, --unencrypted | ||
| + | -P, --pwprompt | ||
| + | -r, --createrole | ||
| + | -R, --no-createrole | ||
| + | -s, --superuser | ||
| + | -S, --no-superuser | ||
| + | -V, --version | ||
| + | --interactive | ||
| + | than using defaults | ||
| + | --replication | ||
| + | --no-replication | ||
| + | -?, --help | ||
| + | |||
| + | Connection options: | ||
| + | -h, --host=HOSTNAME | ||
| + | -p, --port=PORT | ||
| + | -U, --username=USERNAME | ||
| + | -w, --no-password | ||
| + | -W, --password | ||
| + | |||
| + | Report bugs to < | ||
| + | $ createuser kamo | ||
| + | $ createdb --help | ||
| + | createdb creates a PostgreSQL database. | ||
| + | |||
| + | Usage: | ||
| + | createdb [OPTION]... [DBNAME] [DESCRIPTION] | ||
| + | |||
| + | Options: | ||
| + | -D, --tablespace=TABLESPACE | ||
| + | -e, --echo | ||
| + | -E, --encoding=ENCODING | ||
| + | -l, --locale=LOCALE | ||
| + | --lc-collate=LOCALE | ||
| + | --lc-ctype=LOCALE | ||
| + | -O, --owner=OWNER | ||
| + | -T, --template=TEMPLATE | ||
| + | -V, --version | ||
| + | -?, --help | ||
| + | |||
| + | Connection options: | ||
| + | -h, --host=HOSTNAME | ||
| + | -p, --port=PORT | ||
| + | -U, --username=USERNAME | ||
| + | -w, --no-password | ||
| + | -W, --password | ||
| + | --maintenance-db=DBNAME | ||
| + | |||
| + | By default, a database with the same name as the current user is created. | ||
| + | |||
| + | Report bugs to < | ||
| + | $ createdb -E utf-8 -O kamo kamonohashi | ||
| + | $ | ||
| + | </ | ||
| + | |||
| + | データベースのユーザ kamo にはパスワードを設定していません。\\ | ||
| + | 必要ならpsqlコマンドで接続して \password コマンドで設定するか createuser コマンドに -P オプションを付けてユーザ作成時にパスワードも指定してください。\\ | ||
| + | データベース kamonohashi のオーナーを kamo にしてあります。 | ||
| + | |||
| + | psqlコマンドでデータベース kamonohashi へ接続してみます。 | ||
| + | |||
| + | < | ||
| + | $ psql -n kamonohashi kamo | ||
| + | psql (9.6.1) | ||
| + | Type " | ||
| + | |||
| + | kamonohashi=> | ||
| + | No relations found. | ||
| + | kamonohashi=> | ||
| + | kamonohashi(> | ||
| + | kamonohashi(> | ||
| + | kamonohashi(> | ||
| + | CREATE TABLE | ||
| + | kamonohashi=> | ||
| + | List of relations | ||
| + | | ||
| + | --------+------+-------+------- | ||
| + | | ||
| + | (1 row) | ||
| + | |||
| + | kamonohashi=> | ||
| + | INSERT 0 1 | ||
| + | kamonohashi=> | ||
| + | | ||
| + | -----+------ | ||
| + | 1.0 | ABCD | ||
| + | (1 row) | ||
| + | |||
| + | kamonohashi=> | ||
| + | ERROR: | ||
| + | kamonohashi=> | ||
| + | kamonohashi=> | ||
| + | INSERT 0 1 | ||
| + | kamonohashi=> | ||
| + | | ||
| + | -----+---------- | ||
| + | 1.0 | ABCD | ||
| + | 1.0 | あいうえ | ||
| + | (2 rows) | ||
| + | |||
| + | kamonohashi=> | ||
| + | </ | ||
| + | |||
| + | 端末がeuc-jpなので、\encoding コマンドでエンコーディングを指定しています。 | ||
| + | |||
| + | {{tag> 技術資料 FreeBSD Jail PostgreSQL database }} | ||
documents/quick/quick-0018.txt · 最終更新: by k896951
