繼mysql、redis、mongodb、hbase這幾個不同的數據據庫後,又發現了個寶藏數據庫,真的很不錯誒。
PostgreSQL 是一個免費的對象-關係數據庫服務器(ORDBMS),在靈活的BSD許可證下發行,
花了一晚簡單學習了一下,發現和mysql的操作非常相近,那麼學過mysql的上手就會很容易啦,沒學過的也不要緊,常用操作也不是太複雜,但功能卻很強大(ps 命令行成功操作後沒有提示這一點希望以後能改善一下)。
這個是postgresql的官方網站,你可以下載和查看相關教程:https://www.postgresql.org/
這個菜鳥教程也很不錯:https://www.runoob.com/postgresql/postgresql-tutorial.html
Ubuntu 安裝 PostgreSQL
Ubuntu 可以使用 apt-get 安裝 PostgreSQL:
sudo apt-get update
sudo apt-get install postgresql postgresql-client
安裝完畢後,系統會創建一個數據庫超級用戶 postgres,密碼爲空。
# sudo -i -u postgres
這個時候pwd一下,你會發現不是以往的用戶名home路經,而是postgresql數據庫的home目錄
postgres@ubuntu:~$pwd
/var/lib/postgresql
這時使用以下命令進入 postgres,輸出以下信息,說明安裝成功:
~$ psql
psql (9.5.21)
Type "help" for help.
postgres=#
輸入以下命令退出 PostgreSQL 提示符:
\q
PostgreSQL 安裝完成後默認是已經啓動的,但是也可以通過下面的方式來手動啓動服務。
sudo /etc/init.d/postgresql start # 開啓
sudo /etc/init.d/postgresql stop # 關閉
sudo /etc/init.d/postgresql restart # 重啓
windows下的安裝:https://www.runoob.com/postgresql/windows-install-postgresql.html
postgresql的命令行操作可參考官方網站或其他教程
這裏簡單整理一下python操作其增刪改查:
首先要安裝psycopg2 –> pip install psycopg2、 pip install psycopg2-binary
1、連接到數據庫
import psycopg2 #導入相關模塊
#傳入參數 數據庫名 用戶名 用戶密碼 主機地址 端口
conn = psycopg2.connect(database="mydb", user="postgres", password="123456as", host="127.0.0.1", port="5432")
print "Ok"
如果你未設置密碼的話,到這裏你會發現報錯 FATAL: password authentication failed for user "postgres"
安裝PostgreSQL之後,PostgreSQL會在系統中創建一個名爲“postgres”當用戶。PostgreSQL的默認用戶名和數據庫也是“postgres”,不過沒有默認密碼。在安裝PostgreSQL之後可以以默認用戶登錄,也可以創建新當用戶名。
需要手動設置密碼
ubuntu@ubuntu:~ $ sudo su postgres
#切換至postgres or sudo -i -u postgres
postgres@ubuntu /home/ubuntu $ psql postgres#登入默認數據庫
[sudo] passwordfor ubuntu:
psql (9.5.21)
Type "help" for help.
以上命令也可以簡化爲:
ubuntu@ubuntu ~ $ sudo -u postgres psql postgres
登錄之後給默認用戶“postgres”設置密碼
postgres=# \password postgres #給postgres用戶設置密碼
Enter new password:
Enter it again:
postgres=#
psql的連接建立於Unix Socket上默認使用peer authentication,所以必須要用和 數據庫用戶 相同的 系統用戶 進行登錄。
需要將peer authentiction 改爲 md5,並給數據庫設置密碼。修改配置文件,/etc/postgresql/x.x/main/pg_hba.conf
local all postgres peer
// change to
local all postgres md5
重新啓動pgsql,
/etc/init.d/postgresql restart
2、創建表
import psycopg2
conn = psycopg2.connect(database="mydb", user="postgres", password="123456as", host="127.0.0.1", port="5432")
cur = conn.cursor() #建立操作遊標
#傳入的參數是 SQL 建表語句
cur.execute('''CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
print "ok"
conn.commit() #注意,只有commit一下才會生效
conn.close()
3、插入數據
import psycopg2
conn = psycopg2.connect(database="mydb", user="postgres", password="123456as", host="127.0.0.1", port="5432")
cur = conn.cursor()
#讓它ID自動生成
cur.execute("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( 'Paul', 32, 'California', 20000.00 )");
#注意這裏還可以返回插入數據的ID
results = cur.fetchone()
ID = results[0] #返回插入的記錄的id 便於後續操作
conn.commit()
conn.close()
4、刪除數據
import psycopg2
conn = psycopg2.connect(database="mydb", user="postgres", password="123456as", host="127.0.0.1", port="5432")
cur = conn.cursor()
cur.execute("DELETE from COMPANY where ID=2;")
conn.commit
conn.close()
5、更新數據
import psycopg2
conn = psycopg2.connect(database="mydb", user="postgres", password="123456as", host="127.0.0.1", port="5432")
cur = conn.cursor()
cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
conn.commit
conn.close()
6、查詢數據
import psycopg2
conn = psycopg2.connect(database="mydb", user="postgres", password="123456as", host="127.0.0.1", port="5432")
cur = conn.cursor()
cur.execute("SELECT id, name, address, salary from COMPANY")
rows = cur.fetchall() #這裏返回查詢的所有數據集
conn.close()