MirrorNest
← すべてのガイド

PostgreSQL on Ubuntu

A powerful, free, open-source relational database -- installed from Ubuntu's own package repository, with the initial admin role and a first database created.

対象: Ubuntu 22.04 LTS / 24.04 LTSDatabase公式ホームページ ↗公式ドキュメント ↗
このガイドは上記リンク先にあるPostgreSQL on Ubuntu公式ドキュメントをもとに作成されています。ディストリビューションやプロジェクトのバージョンは時間とともに変わるため、本番サーバーでこれらのコマンドを実行する前に、必ず公式ドキュメントで正確なパッケージ名・バージョン名を確認してください。
  1. 1Install PostgreSQL

    sudo apt update
    sudo apt install postgresql postgresql-contrib
  2. 2Switch to the postgres system user

    PostgreSQL creates a `postgres` Linux user during install with full database admin rights -- all initial setup happens through it.

    sudo -i -u postgres
  3. 3Create a database and a role for your app

    createuser --pwprompt myappuser
    createdb --owner=myappuser myappdb

    createuser prompts for a password interactively -- pick a real one, not left blank.

  4. 4Allow password-based connections from your app

    By default PostgreSQL only trusts local Unix-socket connections. Edit pg_hba.conf to allow password auth for TCP connections if your app connects over the network.

    sudo -u postgres psql -c "SHOW hba_file;"
    # Add this line to the file shown above, then restart PostgreSQL: # host myappdb myappuser 127.0.0.1/32 scram-sha-256
    sudo systemctl restart postgresql