1,deployByRuiyIns
rpm -ivh http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm
postgresql yum repo版本 yum.postgresql.org/repopackages.php
2,repository contains many different packages including third party addons.
The most common and important packages are
postgresql-client libraries and client binaries
postgresql-server core database server
postgresql-contrib additional supplied modules
postgresql-devel libraries and headers for C language development
pgadmin3 - pgAdmin III graphical administration utility
3,start database server;
/usr/bin/postgres -D /var/lib/pgsql/data
or
/usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start
4,init Db and start will system boot for Suse Os;
postgresql-setup initdb
systemctl enable postgresql.service
5,分别使用shell和psql 資料庫控制台建立postgresql資料庫和資料庫使用者;
5.01,psql 控制台
su - postgres
psql 進入到postgresql資料庫控制台
create user Ruiy with password '321'; (記住了這地方用單引号)
create database ruiy owner ruiys;
grant all privileges on database ruiy to ruiys;
5.02,shell提示符
添加新使用者和新資料庫
初次安裝後,預設生成一個名為postgres的資料庫和一個名為postgres的資料庫使用者。這裡需要注意的是,同時還生成了一個名為postgres的Linux系統使用者。
下面,我們使用postgres使用者,來生成其他使用者和新資料庫。好幾種方法可以達到這個目的,這裡介紹兩種。
第一種方法,使用PostgreSQL控制台。
首先,建立一個Linux新使用者,可以取你想要的名字,這裡為dbuser。
sudo adduser dbuser
然後,切換到postgres使用者。
sudo su - postgres
下一步,使用psql指令登入PostgreSQL控制台。
psql
這時相當于系統使用者postgres以同名資料庫使用者的身份,登入資料庫,這是不用輸入密碼的。如果一切正常,系統提示符會變為"postgres=#",表示這時已經進入了資料庫控制台。以下的指令都在控制台内完成。
第一件事是使用\password指令,為postgres使用者設定一個密碼。
\password postgres
第二件事是建立資料庫使用者dbuser(剛才建立的是Linux系統使用者),并設定密碼。
CREATE USER dbuser WITH PASSWORD 'password';
第三件事是建立使用者資料庫,這裡為exampledb,并指定所有者為dbuser。
CREATE DATABASE exampledb OWNER dbuser;
第四件事是将exampledb資料庫的所有權限都賦予dbuser,否則dbuser隻能登入控制台,沒有任何資料庫操作權限。
GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser;
最後,使用\q指令退出控制台(也可以直接按ctrl+D)。
\q
第二種方法,使用shell指令行。
添加新使用者和新資料庫,除了在PostgreSQL控制台内,還可以在shell指令行下完成。這是因為PostgreSQL提供了指令行程式createuser和createdb。還是以建立使用者dbuser和資料庫exampledb為例。
首先,建立資料庫使用者dbuser,并指定其為超級使用者。
sudo -u postgres createuser --superuser dbuser
然後,登入資料庫控制台,設定dbuser使用者的密碼,完成後退出控制台。
sudo -u postgres psql
\password dbuser
\q
接着,在shell指令行下,建立資料庫exampledb,并指定所有者為dbuser。
sudo -u postgres createdb -O dbuser exampledb
三、登入資料庫
添加新使用者和新資料庫以後,就要以新使用者的名義登入資料庫,這時使用的是psql指令。
psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432
上面指令的參數含義如下:-U指定使用者,-d指定資料庫,-h指定伺服器,-p指定端口。
輸入上面指令以後,系統會提示輸入dbuser使用者的密碼。輸入正确,就可以登入控制台了。
psql指令存在簡寫形式。如果目前Linux系統使用者,同時也是PostgreSQL使用者,則可以省略使用者名(-U參數的部分)。舉例來說,我的 Linux系統使用者名為ruanyf,且PostgreSQL資料庫存在同名使用者,則我以ruanyf身份登入Linux系統後,可以直接使用下面的指令 登入資料庫,且不需要密碼。
psql exampledb
此時,如果PostgreSQL内部還存在與目前系統使用者同名的資料庫,則連資料庫名都可以省略。比如,假定存在一個叫做ruanyf的資料庫,則直接鍵入psql就可以登入該資料庫。
psql
另外,如果要恢複外部資料,可以使用下面的指令。
psql exampledb < exampledb.sql
四、控制台指令
除了前面已經用到的\password指令(設定密碼)和\q指令(退出)以外,控制台還提供一系列其他指令。
- \h:檢視SQL指令的解釋,比如\h select。
- \?:檢視psql指令清單。
- \l:列出所有資料庫。
- \c [database_name]:連接配接其他資料庫。
- \d:列出目前資料庫的所有表格。
- \d [table_name]:列出某一張表格的結構。
- \du:列出所有使用者。
- \e:打開文本編輯器。
- \conninfo:列出目前資料庫和連接配接的資訊。
五、資料庫操作
基本的資料庫操作,就是使用一般的SQL語言。
# 建立新表
CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);
# 插入資料
INSERT INTO user_tbl(name, signup_date) VALUES('張三', '2013-12-22');
# 選擇記錄
SELECT * FROM user_tbl;
# 更新資料
UPDATE user_tbl set name = '李四' WHERE name = '張三';
# 删除記錄
DELETE FROM user_tbl WHERE name = '李四' ;
# 添加欄位
ALTER TABLE user_tbl ADD email VARCHAR(40);
# 更新結構
ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;
# 更名欄位
ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;
# 删除欄位
ALTER TABLE user_tbl DROP COLUMN email;
# 表格更名
ALTER TABLE user_tbl RENAME TO backup_tbl;
# 删除表格
DROP TABLE IF EXISTS backup_tbl;