LoginSignup
54
58

More than 5 years have passed since last update.

CentOS7にPostgreSQLを構築する方法

Last updated at Posted at 2017-01-16

インストール

PostgreSQLサーバインストール

  • インストール実行

    terminal
    yum -y install postgresql-server
    

初期設定

データベース初期化

  • 初期化コマンドを実行。/var/lib/pgsql/dataに設定ファイルなどが配置される。

    terminal
    postgresql-setup initdb
    

postgresql.confの編集

  • orgファイルバックアップ & 編集

    terminal
    cd /var/lib/pgsql/data
    cp postgresql.conf postgresql.conf.org
    echo "listen_addresses = '*'" >> /var/lib/pgsql/data/postgresql.conf
    
  • 差分確認

    terminal
    diff -u postgresql.conf.org postgresql.conf
    
    --- postgresql.conf.org 2017-01-16 16:57:24.904920137 +0900
    +++ postgresql.conf 2017-01-16 17:00:13.670101306 +0900
    @@ -575,3 +575,4 @@
     #------------------------------------------------------------------------------
    
     # Add settings for extensions here
    +listen_addresses = '*'
    

pg_hba.confの編集

  • orgファイルバックアップ & 編集

    terminal
    cd /var/lib/pgsql/data
    cp pg_hba.conf pg_hba.conf.org
    
    echo "# PostgreSQL Client Authentication Configuration File" >  ./pg_hba.conf
    echo "# ===================================================" >> ./pg_hba.conf
    echo "local all all              trust"                      >> ./pg_hba.conf
    echo "host  all all 127.0.0.1/32 trust"                      >> ./pg_hba.conf
    echo "host  all all ::1/128      trust"                      >> ./pg_hba.conf
    
  • 確認 (orgはコメントだらけでdiff取ると長くなるので、変更後の内容のみ)

    terminal
    cat pg_hba.conf
    
    # PostgreSQL Client Authentication Configuration File
    # ===================================================
    local all all              trust
    host  all all 127.0.0.1/32 trust
    host  all all ::1/128      trust
    

起動

PostgreSQLを起動

  • 起動コマンド実行

    terminal
    service postgresql restart
    

接続確認

PostgreSQLへ接続できることを確認する

  • local接続の可否を確認

    terminal
    psql -U postgres -h 127.0.0.1 -w
    
    psql (9.2.18)
    "help" でヘルプを表示します.
    
    postgres=#
    

postgresユーザーのパスワードを変更

  • SQLを実行

    terminal
    psql -U postgres -c "ALTER ROLE postgres WITH PASSWORD 'your-secure-password'"
    
    ALTER ROLE
    

データベース作成

  • SQLを実行

    terminal
    psql -U postgres -W -c "CREATE DATABASE your_database_name";
    ユーザ postgres のパスワード:
    
    CREATE DATABASE
    
54
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
58