postgresql-check-db-dir 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/sh
  2. # This script verifies that the postgresql data directory has been correctly
  3. # initialized. We do not want to automatically initdb it, because that has
  4. # a risk of catastrophic failure (ie, overwriting a valuable database) in
  5. # corner cases, such as a remotely mounted database on a volume that's a
  6. # bit slow to mount. But we can at least emit a message advising newbies
  7. # what to do.
  8. # source: https://github.com/archlinux/svntogit-packages/blob/packages/postgresql/trunk/postgresql-check-db-dir
  9. PGDATA="$1"
  10. if [ -z "$PGDATA" ]
  11. then
  12. echo "Usage: $0 database-path"
  13. exit 1
  14. fi
  15. # PGMAJORVERSION is major version
  16. PGMAJORVERSION=12
  17. # PREVMAJORVERSION is the previous major version
  18. PREVMAJORVERSION=11
  19. # Check for the PGDATA structure
  20. if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
  21. then
  22. # Check version of existing PGDATA
  23. if [ x`cat "$PGDATA/PG_VERSION"` = x"$PGMAJORVERSION" ]
  24. then
  25. : A-OK
  26. elif [ x`cat "$PGDATA/PG_VERSION"` = x"$PREVMAJORVERSION" ]
  27. then
  28. echo $"An old version of the database format was found."
  29. echo $"You need to dump and reload before using PostgreSQL $PGMAJORVERSION."
  30. echo $"See http://www.postgresql.org/docs/$PGMAJORVERSION/static/upgrading.html"
  31. exit 1
  32. else
  33. echo $"An old version of the database format was found."
  34. echo $"You need to dump and reload before using PostgreSQL $PGMAJORVERSION."
  35. echo $"See http://www.postgresql.org/docs/$PGMAJORVERSION/static/upgrading.html"
  36. exit 1
  37. fi
  38. else
  39. # No existing PGDATA! Warn the user to initdb it.
  40. echo $"\"$PGDATA\" is missing or empty. Use a command like"
  41. echo $" su - postgres -c \"initdb --locale en_US.UTF-8 -D '$PGDATA'\""
  42. echo $"with relevant options, to initialize the database cluster."
  43. exit 1
  44. fi
  45. exit 0