Advertisement
Justman10000

Manage PostgreSQL

Apr 22nd, 2023 (edited)
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.84 KB | None | 0 0
  1. // Install PostgreSQL
  2. # Install dependencies
  3. apt install bison \
  4.             build-essential \
  5.             clang \
  6.             flex \
  7.             libbsd-dev \
  8.             libkrb5-dev \
  9.             libldap2-dev \
  10.             liblz4-dev \
  11.             libossp-uuid-dev \
  12.             libpam0g-dev \
  13.             libperl-dev \
  14.             libreadline-dev \
  15.             libselinux1-dev \
  16.             libssl-dev \
  17.             libxml2-dev \
  18.             libxslt-dev \
  19.             libzstd-dev \
  20.             llvm \
  21.             pkg-config \
  22.             python3 \
  23.             python3-dev \
  24.             python3-pip \
  25.             tcl \
  26.             tcl-dev \
  27.             uuid \
  28.             uuid-dev \
  29.             zlib1g-dev -y
  30.  
  31. # Create user
  32. useradd -M -s /usr/sbin/nologin postgres
  33.  
  34. # The version of your PostgreSQL instance
  35. # To see all exists versions, go to https://ftp.postgresql.org/pub/source
  36. version=$(curl -sL https://ftp.postgresql.org/pub/source/ | grep -oP '[0-9]*\.[0-9]+' | sort -rV | head -n 1)
  37.  
  38. wget https://ftp.postgresql.org/pub/source/v$version/postgresql-$version.tar.gz
  39. gunzip postgresql-$version.tar.gz
  40. tar xvf postgresql-$version.tar
  41. rm -r postgresql-$version.tar
  42. mv postgresql-$version /usr/local/pgsql
  43. cd /usr/local/pgsql
  44. rm -r .* COPYRIGHT HISTORY INSTALL README
  45. ln -s /usr/bin/cc /usr/bin/CMD
  46. bash configure --prefix /usr/local/pgsql --with-CC=CMD --with-llvm --with-tcl --with-perl --with-python --with-gssapi --with-pam --with-ldap --with-selinux --with-libedit-preferred --with-uuid=LIB --with-ossp-uuid --with-libxml --with-libxslt --with-lz4 --with-zstd --with-ssl=LIB --with-openssl
  47. make install
  48.  
  49. ln -s /usr/local/pgsql/bin/* /usr/bin
  50.  
  51. chmod -R 777 /usr/local/pgsql
  52. sudo -u postgres initdb -D /usr/local/pgsql/data
  53. sudo -u postgres pg_ctl -D /usr/local/pgsql/data start
  54.  
  55. # Now you can login in your PostgreSQL instance with:
  56. sudo -u postgres psql
  57.  
  58. # To login with root, do following, after the command above:
  59. sudo -u postgres psql << EOF
  60. CREATE USER root SUPERUSER LOGIN REPLICATION BYPASSRLS;
  61. CREATE DATABASE root OWNER root;
  62. EOF
  63.  
  64. # Let's create a seperate user
  65. # Change USERNAME and PASSWORD
  66. sudo -u postgres psql << EOF
  67. CREATE USER USERNAME \
  68. SUPERUSER \
  69. CREATEDB \
  70. CREATEROLE \
  71. INHERIT \
  72. LOGIN \
  73. REPLICATION \
  74. BYPASSRLS \
  75. PASSWORD \
  76. 'PASSWORD'
  77. EOF
  78.  
  79. # If extensions are needed, do the following:
  80. cd /usr/local/pgsql/contrib/<extension_name>
  81. make install
  82. ## Or for all extensions:
  83. cd /usr/local/pgsql/contrib
  84. make install
  85. ## Then this:
  86. psql # If you have created the root user with its database
  87. sudo -u postgres psql # If you have not created root
  88. CREATE EXTENSION <extension_name>;
  89.  
  90. // Uninstall PostgreSQL
  91. # Stopping PostgreSQL
  92. sudo -u postgres pg_ctl -D /usr/local/pgsql/data stop
  93. # Uninstall PostgreSQL
  94. rm -r /usr/local/pgsql
  95. for bin in $(ls /usr/local/pgsql/bin); do
  96. rm /usr/bin/$bin
  97. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement