root身份安裝
創建用戶 編譯安裝成功后,接下來要做的就是創建一個普通用戶,因為默認超級用戶(root)不能啟動postgresql,所以需要創建一個普通用戶來啟動數據庫,執行以下命令創建用戶: [root@localhost build_dir]# groupadd postgres [root@localhost build_dir]# useradd -g postgres postgres [root@localhost build_dir]# passwd postgres 接下來設置權限,將pg的數據目錄全部賦給postgres用戶,執行以下命令: [root@localhost build_dir]# chown -R postgres:postgres /usr/local/pgsql 創建目錄 [root@localhost build_dir]# mkdir -p /mnt/db1/pgdata/pgsql /mnt/db1/pgdata/pgtbs /mnt/db1/archivelog /backups [root@localhost build_dir]# chmod -R 775 /mnt/db1 [root@localhost build_dir]# chown -R postgres:postgres /mnt/db1 設置環境變量 [root@localhost build_dir]# vi /home/postgres/.bash_profile PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH export PGPORT=8432 export PGHOME=/usr/local/pgsql export PGDATA=/mnt/db1/pgdata/pgsql export PATH=$PGHOME/bin:$PATH export MANPATH=$PGHOME/share/man:$MANPATH export LANG=en_US.UTF-8 export DATE='date +"%Y%m%d%H%M"' export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH export PGHOST=$PGDATA export PGUSER=postgres export PGDATABASE=postgres PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin export PATH 執行如下命令使其生效: [root@localhost build_dir]# source /home/postgres/.bash_profile
准備
由於centos自帶的版本不夠高,直接安裝postgresql會報錯,所以先給llvm和clang升級
# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum localinstall epel-release-latest-7.noarch.rpm
# yum install llvm5.0 llvm5.0-devel clang
yum install -y gcc gcc-c++ openssl openssl-devel readline readline-devel zlib zlib-devel llvm5.0 llvm5.0-devel libxml2-devel libxslt-devel libicu-devel python-devel tcl-devel systemd-devel openldap-devel pam-devel clang perl-ExtUtils-Embed epel-release 實際執行:
yum install -y gcc gcc-c++ epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed readline readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel
但是公司centos6.3電腦上:無法安裝systemd-devel,llvm5.0 ,llvm5.0-devel
configure
# cd build_dir/ vim ../src/Makefile.global.in 修改以下行: COMPILE.c.bc = $(CLANG) -Wno-ignored-attributes $(BITCODE_CFLAGS) $(CPPFLAGS) -flto=thin -emit-llvm -c 修改為: COMPILE.c.bc = $(CLANG) -Wno-ignored-attributes $(BITCODE_CFLAGS) $(CPPFLAGS) -emit-llvm -c -- --prefix 指定默認安裝路徑 [root@localhost build_dir]# ../configure --prefix=/usr/local/pgsql --enable-nls --with-perl --with-python --with-tcl --with-gssapi --with-llvm LLVM_CONFIG='/usr/lib64/llvm5.0/bin/llvm-config' --with-icu --with-openssl --with-pam --with-ldap --with-systemd --with-libxml --with-libxslt
#configure 命令完成后,會發現創建了 config.status 配置文件
# 實際執行:
./configure --prefix=/home/cdrom/work/postgresql11_5432 --enable-nls --with-perl --with-python --with-tcl --with-gssapi --with-llvm LLVM_CONFIG='/usr/lib64/llvm5.0/bin/llvm-config' --with-icu --with-openssl --with-pam --with-ldap --with-systemd --with-libxml --with-libxslt
make make install
初始化數據庫 切換用戶 [root@localhost build_dir]# su - postgres 初始化數據庫 [postgres@localhost ~]$ initdb -D $PGDATA -U postgres --locale=en_US.UTF8 -E UTF8 修改監聽地址 將listen_addresses的值設置成*,使其監聽整個網絡,端口號默認是5432,也可以自己設置。 [postgres@localhost ~]$ vim /mnt/db1/pgdata/pgsql/postgresql.conf 修改內容: listen_addresses = '*' unix_socket_directories = '.' port = 8432 修改客戶端認證方式 [postgres@localhost ~]$ vim /mnt/db1/pgdata/pgsql/pg_hba.conf 添加內容: host all all 0.0.0.0/0 md5 # 其他用戶登陸 設置防火牆規則 #切換回root用戶 [postgres@localhost ~]$ exit [root@localhost build_dir]# firewall-cmd --zone=public --add-port=8432/tcp --permanent [root@localhost build_dir]# firewall-cmd --reload 啟動數據庫 [root@localhost build_dir]# su - postgres 啟動 [postgres@localhost ~]$ pg_ctl -D /mnt/db1/pgdata/pgsql -l /mnt/db1/archivelog/pgsql.log start 停止 [root@localhost postgres]# pg_ctl -D /mnt/db1/pgdata/pgsql/ -s -m fast stop 連接測試 [postgres@localhost ~]$ psql 查詢所有用戶 postgres=# select * from pg_user; postgres=# select * from pg_roles; 查詢權限 postgres=# select * from information_schema.table_privileges where grantee='cc'; 查看有哪些數據庫 postgres=# \l 相當與mysql的show databases; postgres=# select datname from pg_database; 相當於mysql的show tables, public 是默認的schema的名字 postgres=# SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'; 相當與mysql的describe table_name, 'table_name'是要查詢的表的名字 postgres=# SELECT column_name FROM information_schema.columns WHERE table_name ='table_name'; 退出 postgres=# \q psql 是 PostgreSQL 的客戶端程序,要連接 PostgreSQL 數據庫,我們需要指定以下內容: -d or --dbname 數據庫名 -h or --host 主機名 -p or --port 端口號,默認5432 端口 -U or --username 用戶名 [postgres@localhost ~]$ psql -h localhost -p 8432 -U postgres 設置postgres用戶密碼 [postgres@localhost ~]$ psql postgres=# ALTER USER postgres WITH encrypted PASSWORD 'new password'; postgres=# \q [postgres@localhost ~]$ psql -h localhost -p 8432 -U postgres 設置開機自啟動 設置啟動配置 vim /usr/lib/systemd/system/postgresql-11.service 添加內容: [Unit] Description=PostgreSQL 11 database server Documentation=https://www.postgresql.org/docs/11/static/ After=syslog.target After=network.target [Service] Type=notify User=postgres Group=postgres # Location of database directory Environment=PGDATA=/mnt/db1/pgdata/pgsql/ # Where to send early-startup messages from the server (before the logging # options of postgresql.conf take effect) # This is normally controlled by the global default set by systemd # StandardOutput=syslog # Disable OOM kill on the postmaster OOMScoreAdjust=-1000 Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj Environment=PG_OOM_ADJUST_VALUE=0 # ExecStartPre=/usr/pgsql-11/bin/postgresql-11-check-db-dir ${PGDATA} ExecStart=/usr/local/pgsql/bin/postmaster -D ${PGDATA} ExecReload=/bin/kill -HUP $MAINPID # ExecStart=/usr/local/pgsql9.4/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300 # ExecStop=/usr/local/pgsql9.4/bin/pg_ctl stop -D ${PGDATA} -s -m fast # ExecReload=/usr/local/pgsql9.4/bin/pg_ctl reload -D ${PGDATA} -s KillMode=mixed KillSignal=SIGINT # Do not set any timeout value, so that systemd will not kill postmaster # during crash recovery. TimeoutSec=0 [Install] WantedBy=multi-user.target 添加可執行權限 [root@localhost postgres]# chmod 754 /usr/lib/systemd/system/postgresql-11.service 設置開機自啟動 自動啟動 [root@localhost postgres]# systemctl enable postgresql-11.service 啟動 [root@localhost postgres]# systemctl start postgresql-11.service 停止某服務 [root@localhost postgres]# systemctl stop postgresql-11.service 不自動啟動 [root@localhost postgres]# systemctl disable postgresql-11.service 檢查服務狀態(服務詳細信息) systemctl status postgresql-11.service 檢查服務狀態(僅顯示是否Active) systemctl is-active postgresql-11.service 顯示所有已啟動的服務 systemctl list-units --type=service
################################################################
centos6.3上安裝如下:
systemd-devel,llvm5.0 ,llvm5.0-devel,沒有安裝上。 ./configure --prefix=/home/postgres/postgres11_5434 --enable-nls --with-perl --with-python --with-tcl --with-gssapi --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt
同時在make的時候會出錯,需要將postgresql-11.5/src/test/isolation目錄下面的specs目錄給換個名稱,但是目前還不知道有什么影響
make -j4 make install