PostgreSQL通常也簡稱Postgres,是一個關系型數據庫管理系統,適用於各種Linux操作系統、Windows、Solaris、BSD和Mac OS X。本文基於CentOS7,使用yum源安裝,安裝版本9.4。為具體操作步驟見下文。
yum安裝PostgreSQL
下載PostgreSQL源:
rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm
登錄官網,選擇適合自己的版本,本文選擇的是PostgreSQL 9.4對應的CentOS7-x86_64版本。
執行安裝命令:
yum install postgresql94-server postgresql94-contrib
驗證是否安裝成功:
rpm -aq| grep postgres
執行結果如下:
postgresql94-libs-9.4.12-1PGDG.rhel7.x86_64
postgresql94-9.4.12-1PGDG.rhel7.x86_64
postgresql94-server-9.4.12-1PGDG.rhel7.x86_64
postgresql94-contrib-9.4.12-1PGDG.rhel7.x86_64
說明正確安裝。
初始化數據庫
/usr/pgsql-9.4/bin/postgresql94-setup initdb
一定要先初始化數據庫,否則啟動的時候會報如下錯誤:
postgresql-check-db-dir[2994]: "/var/lib/pgsql/data" is missing or empty.
postgresql-check-db-dir[2994]: Use "postgresql-setup initdb" to initialize the database cluster.
啟動服務並設置為開機啟動
systemctl enable postgresql-9.4
systemctl start postgresql-9.4
訪問PostgreSQL
初次安裝后,默認生成一個名為postgres的數據庫和一個名為postgres的數據庫用戶。這里需要注意的是,同時還生成了一個名為postgres的Linux系統用戶。登錄PostgreSQL時必須使用數據庫中已有的用戶,否則提示如下錯誤:
psql: 致命錯誤: 角色 "root" 不存在
先登錄到postgres用戶:
su - postgres
出現如下提示:
上一次登錄:四 7月 27 14:33:23 CST 2017pts/0 上
-bash-4.2$
使用psql
登錄:
-bash-4.2$ psql
psql (9.4.12)
輸入 "help" 來獲取幫助信息.
使用\q
退出:
postgres=# \q
-bash-4.2$