質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Q&A

解決済

4回答

53154閲覧

CentOS7でsystemctl start postgresがエラー

doc4

総合スコア66

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

0グッド

0クリップ

投稿2016/05/23 03:18

編集2016/05/23 13:57

環境
Virtualbox + vagrant
ホストOS: Windows7 64bit
ゲストOS: Linux version 3.10.0-327.18.2.el7.x86_64
CentOS Linux release 7.2.1511 (Core)
postgresqlのバージョン: psql (PostgreSQL) 9.2.15

CentOS7でpostgresqlをインストールしました。
下記のようにインストールとinitをおこない

sudo yum -y install postgresql-server postgresql-contrib postgresql-devel sudo postgresql-setup initdb

/var/lib/pgsql/data/pg_hba.confを編集しました。

その後、下記コマンドを実行したのですがエラーになってしまいます。

[vagrant@localhost]~/% sudo systemctl start postgresql Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details. [vagrant@localhost]~/% sudo systemctl status postgresql master ● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2016-05-23 10:51:01 JST; 48s ago Process: 3054 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE) Process: 3048 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) May 23 10:51:00 localhost.localdomain systemd[1]: Starting PostgreSQL database server... May 23 10:51:01 localhost.localdomain pg_ctl[3054]: pg_ctl: could not start server May 23 10:51:01 localhost.localdomain systemd[1]: postgresql.service: control process exited, code=exited status=1 May 23 10:51:01 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server. May 23 10:51:01 localhost.localdomain systemd[1]: Unit postgresql.service entered failed state. May 23 10:51:01 localhost.localdomain systemd[1]: postgresql.service failed.

いろいろ試して、下記のコマンドで起動できることを確認しました。

[vagrant@localhost]~% sudo -u postgres /usr/bin/pg_ctl start -D ${PGDATA} could not change directory to "/home/vagrant" server starting

しかし、まだ2つ問題が残っています。

  1. 引き続きsystemctlからpostgresqlを起動出来ないため、systemctl enable postgresql.service

しても自動起動しません。
2. systemctl start postgresqlとしたときは/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300が実行されるようなのですが、PGPORTはpostgresユーザでprintenvしても設定されていませんでした。そのためsystemctl start postgresqlはpオプションの引数なしでエラーになってしまいます。

どちらの問題も私の知識で無理やり回避することはできますがスマートな方法が知りたいので、「普通こうやるよ」という方法をどなたか教えていただけないでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takyafumin

2016/05/23 03:39 編集

virtualbox+vagrantのような仮想環境上に構築していますか? 事象が発生している環境をより詳細に記載いただけると回答がつきやすいかと思います。 * 仮想環境のバージョン(virtualboxやvagrantのバージョン) * ホストOS, ゲストOSバージョン(CentOSのバージョン), postgresqlのバージョン また起動時のエラー内容が詳細に分かると良いと思いますので、起動エラー時の下記のログ内容も追記すると良いかと思います。 * システムログ: /var/log/messages * postgresqlログ: /var/lib/pgsql/data/pg_log/ 配下のログファイル
guest

回答4

0

systemd で起動するなら systemd のユニットの Environment とか EnvironmentFile とかで環境変数が指定されています。

下記を実行してみたところ、

sudo systemctl show postgresql | grep Environment

わたしの環境では次の通りになりました。

Environment=PGPORT=5432 PGDATA=/var/lib/pgsql/data

お使いの環境で、下記を実行してどのような結果になるかをご確認いただくと宜しいかと思われます。

sudo systemctl show postgresql | grep Environment

投稿2016/05/23 04:25

ngyuki

総合スコア4514

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

doc4

2016/05/23 14:35

ngyukiさん、ご回答ありがとうございます! 教えて頂いたコマンドを実行したところ、ngyukiさんと同じ出力結果となりました。 てっきり、PGPORTやPGDATAはpostgresユーザの環境変数として設定されているものと思っていたのですが、systemdの環境変数はまた別だったのですね。 環境変数に問題無いことがわかったので、systemdではなく生のコマンドを色々試していったところオプションの設定方法が悪いということに気づきました。下記のようにpostgresユーザでログインしてpg_ctlのオプションをいじってみました。上述のログのように ` -o -p 5432`で実行してしまうと引数が多すぎるとのエラーがでてしまいます。/usr/lib/systemd/postgresql.serviceを開くと、ExecStartには、`ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300`と記述されており、`-p 5432`を`"`で囲む記述となっていました。そこで、実際にオプションを`"-p 5432"`として実行したところ`server started`と表示され起動ができたようです。 ``` [vagrant@localhost]~% su - postgres Password: Last login: Mon May 23 23:21:15 JST 2016 on pts/1 -bash-4.2$ /usr/bin/pg_ctl start -D /var/lib/pgsql/data -o -p 5432 -w -t 300 pg_ctl: too many command-line arguments (first is "start") Try "pg_ctl --help" for more information. -bash-4.2$ /usr/bin/pg_ctl start -D /var/lib/pgsql/data -o "-p 5432" -w -t 300 waiting for server to start.... done server started ``` ここから、yum installで生成される/usr/lib/systemd/postgresql.serviceのExecStartでは、そもそもpostgresqlを起動できないのではないかと思ったのですが私の考え方はあっているのでしょうか?または、他の方々はこのようなExecStartの記述でも、`-p 5432`でエラーが発生せずに起動できるのでしょうか?
ngyuki

2016/05/23 16:10

お使いの環境で `postgresql.service` を開くと ExecStartは下記のようになっていて、 ``` ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300 ``` そして手動で起動した時も下記で起動できたのですよね? ``` /usr/bin/pg_ctl start -D /var/lib/pgsql/data -o "-p 5432" -w -t 300 ``` yum install でインストールされた `postgresql.service` の記述は正しいということではないのでしょうか?
doc4

2016/05/23 17:56

私が考えていたのは、`sudo systemctl status postgresql`で出力されるログが `Process: 3054 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE)`となっていたため、`sudo systemctl start postgresql`で実行されるpg_ctlでは`-p ${PGPORT}`が"で囲まれていないのではないかと考えていました。 ただ、その後postgresql systemd ExecStartあたりでググると起動している場合も同様のログが出ているようですので、仰るとおりpostgresql.serviceの記述がおかしいということはなさそうです。 http://kanjuku-tomato.blogspot.jp/2014/09/centos-7postgresql-92rpmsystemd.html
guest

0

解決方法がわからなかったため、ほぼ同じ環境で成功しているの手順に従うことにしました。
http://kanjuku-tomato.blogspot.jp/2014/09/centos-7postgresql-92rpmsystemd.html

なぜうまくいかないのか理由が知りたかったのですが諦めます。
ご回答頂いた方々ありがとうございました!

投稿2016/05/24 06:08

doc4

総合スコア66

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

質問者様と同バージョンのCentOSをvagrantで導入し環境確認しました。
yum install で導入される「postgresql.service」では"-p ${PGPORT}"にて正しくポート番号を指定して起動するようになっていますね。

差分確認の参考のため、ファイル内容をそのまま記載します。

  • /usr/lib/systemd/system/postgresql.service
[vagrant@centos7 ~]$ cat /usr/lib/systemd/system/postgresql.service # It's not recommended to modify this file in-place, because it will be # overwritten during package upgrades. If you want to customize, the # best way is to create a file "/etc/systemd/system/postgresql.service", # containing # .include /lib/systemd/system/postgresql.service # ...make your changes here... # For more info about custom unit files, see # http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F # For example, if you want to change the server's port number to 5433, # create a file named "/etc/systemd/system/postgresql.service" containing: # .include /lib/systemd/system/postgresql.service # [Service] # Environment=PGPORT=5433 # This will override the setting appearing below. # Note: changing PGPORT or PGDATA will typically require adjusting SELinux # configuration as well; see /usr/share/doc/postgresql-*/README.rpm-dist. # Note: do not use a PGDATA pathname containing spaces, or you will # break postgresql-setup. # Note: in F-17 and beyond, /usr/lib/... is recommended in the .include line # though /lib/... will still work. [Unit] Description=PostgreSQL database server After=network.target [Service] Type=forking User=postgres Group=postgres # Port number for server to listen on Environment=PGPORT=5432 # Location of database directory Environment=PGDATA=/var/lib/pgsql/data # Where to send early-startup messages from the server (before the logging # options of postgresql.conf take effect) # This is normally controlled by the global default set by systemd # StandardOutput=syslog # Disable OOM kill on the postmaster OOMScoreAdjust=-1000 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300 ExecStop=/usr/bin/pg_ctl stop -D ${PGDATA} -s -m fast ExecReload=/usr/bin/pg_ctl reload -D ${PGDATA} -s # Give a reasonable amount of time for the server to start up/shut down TimeoutSec=300 [Install] WantedBy=multi-user.target [vagrant@centos7 ~]$

投稿2016/05/23 15:56

takyafumin

総合スコア2335

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

doc4

2016/05/23 17:45

takayafuminさん、ご回答ありがとうございます!diffを取ってみましたが、私のローカルのファイルと同一でした。postgresql.serviceファイルには問題は無いということですね。。後は、各ファイルの権限等をチェックしてみます。
guest

0

自己解決

ngyukiさん、ご回答ありがとうございます!

教えて頂いたコマンドを実行したところ、ngyukiさんと同じ出力結果となりました。
てっきり、PGPORTやPGDATAはpostgresユーザの環境変数として設定されているものと思っていたのですが、systemdの環境変数はまた別だったのですね。

環境変数に問題無いことがわかったので、systemdではなく生のコマンドを色々試していったところオプションの設定方法が悪いということに気づきました。下記のようにpostgresユーザでログインしてpg_ctlのオプションをいじってみました。上述のログのように -o -p 5432で実行してしまうと引数が多すぎるとのエラーがでてしまいます。/usr/lib/systemd/postgresql.serviceを開くと、ExecStartには、ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300と記述されており、-p 5432"で囲む記述となっていました。そこで、実際にオプションを"-p 5432"として実行したところserver startedと表示され起動ができたようです。

[vagrant@localhost]~% su - postgres Password: Last login: Mon May 23 23:21:15 JST 2016 on pts/1 -bash-4.2$ /usr/bin/pg_ctl start -D /var/lib/pgsql/data -o -p 5432 -w -t 300 pg_ctl: too many command-line arguments (first is "start") Try "pg_ctl --help" for more information. -bash-4.2$ /usr/bin/pg_ctl start -D /var/lib/pgsql/data -o "-p 5432" -w -t 300 waiting for server to start.... done server started

ここから、yum installで生成される/usr/lib/systemd/postgresql.serviceのExecStartでは、そもそもpostgresqlを起動できないのではないかと思ったのですが私の考え方はあっているのでしょうか?または、他の方々はこのようなExecStartの記述でも、-p 5432でエラーが発生せずに起動できるのでしょうか?

投稿2016/05/23 14:35

doc4

総合スコア66

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問