YUM安裝:
------------------------------------------------------------------------------------------
1、安裝Postgresql安裝源:
# yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm # yum clean all # yum makecache
2、安裝數據庫:
# yum install postgresql12 postgresql12-server
3、始化數據庫:
# service postgresql-12 initdb
4、啟動數據庫服務:
# service postgresql-12 start # chkconfig postgresql-12 on
5、登錄數據庫:
# su postgres $ psql
6、修改管理員賬號密碼:
=# alter user postgres with encrypted password 'mypass';
7、退出數據庫:
=# \q 或者 \quit
源碼編譯安裝:
------------------------------------------------------------------------------------------
1、官網下載Postgresql:
# wget https://ftp.postgresql.org/pub/source/v12.3/postgresql-12.3.tar.gz
2、安裝依賴包:
# yum -y install bison flex readline-devel zlib-devel
3、解壓源碼包:
# tar -xzvf postgresql-12.3.tar.gz
4、編譯源碼包:
# ./configure --prefix=/usr/local/postgresql # make # make install
5、增加postgres賬戶:
# groupadd postgres # useradd -g postgres postgres
6、新增DATA目錄:
# mkdir /usr/local/postgresql/data # chown -R postgres.postgres /usr/local/postgresql
7、數據庫系統初始化:
$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
8、增加環境變量:
$ vim /home/postgres/.bash_profile export PGHOME=/usr/local/postgresql export PGDATA=/usr/local/postgresql/data export PATH=$PATH:$HOME/bin:$PGHOME/bin
$ source /home/postgres/.bash_profile
9、啟動數據庫服務:
$ /usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data/ -l logfile start $ /usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data/ stop $ /usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data/ -m fast restart
10、添加開機啟動項目:
$ /usr/local/src/postgresql-12.3/contrib/start-scripts/linux /etc/init.d/postgresql $ chmod a+x /etc/init.d/postgresql
$ vim /etc/init.d/postgresql 按實際情況修改如下項目: # Installation prefix prefix=/usr/local/postgresql # Data directory PGDATA="/usr/local/postgresql/data"
添加開機項目:
$ chkconfig --add postgresql
11、設置管理員用戶密碼:
$ psql -U postgres =# ALTER USER postgres with encrypted password '17track'; =# \quit
常用命令:
--------------------------------------------------------------------------
查看所有數據庫:
=# \l 或者 \list
列出當前數據庫的表:
=# \d
創建數據庫:
=# create database mydb;
切換數據庫:
=# \c mydb
在當前數據庫插入表:
=# create table test(id int,body varchar(100));
新建用戶:
=# create user test with password 'test';
修改賬號密碼:
=# alter user postgres with encrypted password 'mypass';
賦予指定賬號到指定數據庫權限:
=# grant all privileges on database mydb to test;
移除指定賬號到指定數據庫權限:
=# revoke all privileges on database mydb to test;
[THE END]
