參考:https://www.postgresql.org/docs/current/install-procedure.html 完事開頭難!!!如果想瞭解一門技術,看文檔必不可少,實操更不可少,這篇博文記錄了自己學習postgesql的測試安裝文檔,由於對pg的參數瞭解甚少,目前使用的預設的參數 ...
參考:https://www.postgresql.org/docs/current/install-procedure.html
完事開頭難!!!如果想瞭解一門技術,看文檔必不可少,實操更不可少,這篇博文記錄了自己學習postgesql的測試安裝文檔,由於對pg的參數瞭解甚少,目前使用的預設的參數。
1、下載安裝介質、解壓並配置軟連接
https://www.postgresql.org/download/ 找到 Source code-> 點擊 file browser鏈接 https://www.postgresql.org/ftp/source/ 選擇需要下載的對應版本源碼安裝介質 wget https://ftp.postgresql.org/pub/source/v10.5/postgresql-10.5.tar.gz 上傳到/usr/local/src/postgresql-10.5.tar.gz tar zxvf /usr/local/src/postgresql-10.5.tar.gz -C /usr/local ln -s /usr/local/postgresql-10.5 /usr/local/pgsql
2、創建用戶和目錄並授權
useradd postgresql echo "123456" | passwd --stdin postgresql mkdir -pv /dbdata/pgsql10.5/pg5432/data chown postgresql:postgresql /usr/local/pgsql chown postgresql:postgresql /dbdata/pgsql10.5/pg5432/data
3、用戶postgresql配置環境變數
export PGHOME=/usr/local/pgsql export PGDATA=/dbdata/pgsql10.5/pg5432/data export PGUSER=postgresql LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:/usr/include export LD_LIBRARY_PATH export PATH=$PGHOME/bin:$PATH:. export MANPATH=$PGHOME/share/man:$MANPATH
4、編譯並安裝
cd /usr/local/pgsql ./configure --prefix=/usr/local/pgsql --with-pgport=5432 gmake gmake world gmake install gmake install-world 說明: /usr/local/pgsql/configure --help | grep size --with-blocksize=BLOCKSIZE set table block size in kB [8] --with-segsize=SEGSIZE set table segment size in GB [1] --with-wal-blocksize=BLOCKSIZE set WAL block size in kB [8] --with-wal-segsize=SEGSIZE set WAL segment size in MB [16] 這些參數在編譯的時候可以指定,後續初始化後就不可以重新設置了,除非重新安裝的時候指定這些參數。
5.初始化
[postgresql@lxdnode2 ~]$ initdb -E UTF8 -D $PGDATA -U admin -W --locale=C The files belonging to this database system will be owned by user "postgresql". This user must also own the server process. The database cluster will be initialized with locale "C". The default text search configuration will be set to "english". Data page checksums are disabled. Enter new superuser password: Enter it again: fixing permissions on existing directory /dbdata/pgsql10.5/pg5432/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting dynamic shared memory implementation ... posix creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /dbdata/pgsql10.5/pg5432/data -l logfile start
6.啟動資料庫
pg_ctl -D /dbdata/pgsql10.5/pg5432/data -l logfile start
7.連接資料庫
[postgresql@lxdnode2 data]$ psql
psql: FATAL: database "postgresql" does not exist
登錄pg資料庫的時候如果不指定-d參數預設就會找跟當前操作系統用戶同名的database
[postgresql@lxdnode2 data]$ psql -d postgres -U admin
psql (10.5)
Type "help" for help.
postgres=#
這裡留下一個伏筆,如果剛剛接觸pg的同學可能會好奇,明明設置了admin用戶的密碼了,為什麼登錄的時候沒有要求密碼驗證就可以登錄進去呢?