class Postgresql < Formula desc "Object-relational database system" homepage "https://www.postgresql.org/" url "https://ftp.postgresql.org/pub/source/v13.1/postgresql-13.1.tar.bz2" sha256 "12345c83b89aa29808568977f5200d6da00f88a035517f925293355432ffe61f" license "PostgreSQL" head "https://github.com/postgres/postgres.git" livecheck do url "https://www.postgresql.org/docs/current/static/release.html" regex(/Release v?(\d+(?:\.\d+)*)/i) end bottle do sha256 "e4d523c7171f265b340df22a88ff78e6fba4aed46afcf3f4e5bd4ac4b94e8a16" => :big_sur sha256 "13939e578f0a48c78966c2527dc48391b19650b51f7489767b5237e3bab16793" => :catalina sha256 "1826c98f6d117bd040fbb307c1c95dfa2dee6ff8647ec8010e1b79386aa59eb0" => :mojave end depends_on "pkg-config" => :build depends_on "icu4c" # GSSAPI provided by Kerberos.framework crashes when forked. # See https://github.com/Homebrew/homebrew-core/issues/47494. depends_on "krb5" depends_on "openssl@1.1" depends_on "readline" uses_from_macos "libxml2" uses_from_macos "libxslt" uses_from_macos "perl" on_linux do depends_on "util-linux" end def install ENV.prepend "LDFLAGS", "-L#{Formula["openssl@1.1"].opt_lib} -L#{Formula["readline"].opt_lib}" ENV.prepend "CPPFLAGS", "-I#{Formula["openssl@1.1"].opt_include} -I#{Formula["readline"].opt_include}" args = %W[ --disable-debug --prefix=#{prefix} --datadir=#{HOMEBREW_PREFIX}/share/postgresql --libdir=#{HOMEBREW_PREFIX}/lib --includedir=#{HOMEBREW_PREFIX}/include --sysconfdir=#{etc} --docdir=#{doc} --enable-thread-safety --with-bonjour --with-gssapi --with-icu --with-ldap --with-libxml --with-libxslt --with-openssl --with-pam --with-perl --with-tcl --with-uuid=e2fs ] # PostgreSQL by default uses xcodebuild internally to determine this, # which does not work on CLT-only installs. args << "PG_SYSROOT=#{MacOS.sdk_path}" if MacOS.sdk_root_needed? system "./configure", *args system "make" system "make", "install-world", "datadir=#{pkgshare}", "libdir=#{lib}", "pkglibdir=#{lib}/postgresql", "includedir=#{include}", "pkgincludedir=#{include}/postgresql", "includedir_server=#{include}/postgresql/server", "includedir_internal=#{include}/postgresql/internal" end def post_install return if ENV["CI"] (var/"log").mkpath postgresql_datadir.mkpath system "#{bin}/initdb", "--locale=C", "-E", "UTF-8", postgresql_datadir unless pg_version_exists? end def postgresql_datadir var/"postgres" end def postgresql_log_path var/"log/postgres.log" end def pg_version_exists? (postgresql_datadir/"PG_VERSION").exist? end def caveats <<~EOS To migrate existing data from a previous major version of PostgreSQL run: brew postgresql-upgrade-database This formula has created a default database cluster with: initdb --locale=C -E UTF-8 #{postgresql_datadir} For more details, read: https://www.postgresql.org/docs/#{version.major}/app-initdb.html EOS end plist_options manual: "pg_ctl -D #{HOMEBREW_PREFIX}/var/postgres start" def plist <<~EOS KeepAlive Label #{plist_name} ProgramArguments #{opt_bin}/postgres -D #{postgresql_datadir} RunAtLoad WorkingDirectory #{HOMEBREW_PREFIX} StandardOutPath #{postgresql_log_path} StandardErrorPath #{postgresql_log_path} EOS end test do system "#{bin}/initdb", testpath/"test" unless ENV["CI"] assert_equal "#{HOMEBREW_PREFIX}/share/postgresql", shell_output("#{bin}/pg_config --sharedir").chomp assert_equal "#{HOMEBREW_PREFIX}/lib", shell_output("#{bin}/pg_config --libdir").chomp assert_equal "#{HOMEBREW_PREFIX}/lib/postgresql", shell_output("#{bin}/pg_config --pkglibdir").chomp end end