Ubuntu14.04安裝PostpreSQL9.3.5記錄

安裝參考:http://www.postgresql.org/download/linux/ubuntu/linux

y@y:~$ sudo apt-get install postgresql-9.3  postgresql-client-9.3  postgresql-contrib-9.3 postgresql-server-dev-9.3  pgadmin3 

* Starting PostgreSQL 9.3 database server [ OK ]
正在設置 postgresql-contrib-9.3 (9.3.5-0ubuntu0.14.04.1) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...sql

表示安裝成功。數據庫

配置:ubuntu

1:使用psql客戶端登陸vim

y@y:~$ sudo -u postgres psql
psql (9.3.5)
Type "help" for help.

2:修改PostgreSQL默認用戶postgres的登陸密碼post

PostgreSQL數據默認會建立一個postgres的數據庫用戶做爲數據庫的管理員,密碼是隨機的,因此這裏要修改密碼。this

postgres=# alter user postgres with password 'postgres';
ALTER ROLE

3:退出PostgreSQL psql客戶端spa

postgres-# \q
could not save history to file "/var/lib/postgresql/.psql_history": 沒有那個文件或目錄
(從新登陸就能夠了,因爲第一次文件是不存在的)

4:修改linux系統的postgres用戶的密碼命令行

y@y:~$ sudo passwd -d postgres 
passwd:密碼過時信息已更改。
y@y:~$ sudo -u postgres passwd 
輸入新的 UNIX 密碼: 
從新輸入新的 UNIX 密碼: 
passwd:已成功更新密碼

5:修改PostgresSQL數據庫配置實現遠程訪問rest

y@y:~$ sudo vim /etc/postgresql/9.3/main/postgresql.conf 
(1).監放任何地址訪問,修改鏈接權限
#listen_addresses = ‘localhost’改成 listen_addresses = ‘*’        
(2).啓用密碼驗證
#password_encryption = on改成password_encryption = on
(3).可訪問的用戶ip段
y@y:/etc/postgresql/9.3/main$ sudo vim pg_hba.conf
並在文檔末尾加上如下內容
# to allow your client visiting postgresql server
host all all 0.0.0.0 0.0.0.0 md5
(4):重啓PostgreSQL數據庫
y@y:~$ /etc/init.d/postgresql restart
 * Restarting PostgreSQL 9.3 database server                                     * Error: You must run this program as the cluster owner (postgres) or root
                                                                         [fail]
重啓失敗,提示須要root權限
y@y:~$ sudo /etc/init.d/postgresql restart
 * Restarting PostgreSQL 9.3 database server                                      [ OK ] 
y@y:~$ 

6:管理PostgreSQL用戶和數據庫

(1)登陸postgre SQL數據庫
y@y:~$ sudo psql -U postgres -h 127.0.0.1
Password for user postgres:
(2)建立新用戶test,但不給建數據庫的權限
用戶名要用雙引號,以區分大小寫,密碼不用
postgres=# create user "test" password 'test' nocreatedb;
CREATE ROLE
(3)創建數據庫,並指定全部者
postgres=# create database "testdb" with owner="test";
CREATE DATABASE

(4)在外部命令行的管理命令
postgres=# -u postgres createuser -D -P test1
-D該用戶沒有建立數據庫的權利,-P提示輸入密碼,選擇管理類型y/n
postgres-# -u postgres createdb -O test1 db1
-O設定全部者爲test1
登陸數據庫
y@y:~$ psql -U test -d testdb -h 127.0.0.1 -p 5432
Password for user test: 
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

testdb=> 

7:基本的數據庫操做,就是使用通常的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;

相關文章
相關標籤/搜索