Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 平台 PostgreSQL 安裝、啟動關閉與資料備份還原

一、安裝

# 1. 下載原始碼:https://www.postgresql.org/ftp/source/
# 2. 解壓
tar zxvf postgresql-8.3.7.tar.gz
cd postgresql-8.3.7
# 3. 配置
./configure --prefix=/usr/local/pgsql
# 4. 編譯
make
# 5. 安裝
make install
# 6. 建立使用者組與使用者
groupadd postgres
useradd -g postgres postgres
# 7. 建立資料存放目錄並授權
mkdir /usr/local/pgsql/data
cd /usr/local/pgsql
chown postgres.postgres data

二、初始化與啟動

su - postgresql
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data      # 初始化
/usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data  # 啟動

三、監聽位址與遠端連線

vi /usr/local/pgsql/data/postgresql.conf
listen_addresses = '*'
port = 5432

vi /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 trust

防火牆放行 5432:

vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
service iptables restart

四、設定開機自啟

cd /etc/rc.d/init.d
cp (解壓目錄)/postgresql-8.3.7/contrib/start-scripts/linux postgresql
chmod +x postgresql
vi postgresql
prefix=/usr/local/pgsql
PGDATA="/usr/local/pgsql/data"
PGUSER=postgres
PGLOG="/var/log/pgsql.log"
chkconfig --add postgresql
service postgresql start

五、手動啟動與關閉

su - postgres
pg_ctl start -D /usr/local/pgsql/data -o -i
pg_ctl stop -D /usr/local/pgsql/data -o -i
pg_ctl restart -D /usr/local/pgsql/data -o -i

六、psql 使用

./psql -n dbname -U usrname
stay81=# select count(*) from table1;
 count
-------
 5986
(1 row)
stay81=# \q      # 結束後記得退出,否則連線會一直掛著

七、資料備份與還原

# 導出單表
./pg_dump -U postgres -t testTable -d testDB > testTable.dump
# 導入
./psql -U postgres -d testDB < testTable.dump
# 以自訂格式導出(-F c)並顯示詳細(-v)
./pg_dump -U postgres -d stay81 -t item_search -F c -v > item_search.dump
# 以 pg_restore 還原
./pg_restore -U postgres -d testDB < testTable.dump
# 一台主機直接轉儲到另一台主機
pg_dump -h host1 -p post1 dbname | psql -h host2 -p post1 dbname
./pg_dump -U postgres -d testDB1 -t testTable | ./psql -U postgres -d testDB2
Copyright © Linux教程網 All Rights Reserved