PostgreSQL是一種特性很是齊全的自由軟件的對象-關係型數據庫管理系統(ORDBMS),是以加州大學計算機系開發的POSTGRES,4.2版本爲基礎的對象關係型數據庫管理系統。POSTGRES的許多領先概念只是在比較遲的時候纔出如今商業網站數據庫中。PostgreSQL支持大部分的SQL標準而且提供了不少其餘現代特性,如複雜查詢、外鍵、觸發器、視圖、事務完整性、多版本併發控制等。一樣,PostgreSQL也能夠用許多方法擴展,例如經過增長新的數據類型、函數、操做符、彙集函數、索引方法、過程語言等。另外,由於許可證的靈活,任何人均可以以任何目的無償使用、修改和分發PostgreSQL。linux
1.下載PostgreSQL https://www.enterprisedb.com/download-postgresql-binariessql
下載相應版本經過ftp上傳到服務器的/home路徑下,而後解壓,獲得pgsql文件夾數據庫
2.建立pgsql用戶並建立密碼:bootstrap
1 [root@localhost home]# useradd postgres 2 [root@localhost home]# passwd postgres 3 更改用戶 postgres 的密碼 。 4 新的 密碼: 5 無效的密碼: 密碼包含用戶名在某些地方 6 從新輸入新的 密碼: 7 passwd:全部的身份驗證令牌已經成功更新。
注意:這裏設置密碼是linux用戶postgres的登陸密碼,不是pgsql服務器的密碼bash
3.建立pgsql的數據目錄並賦權限服務器
1 # 當前目錄在/home下 2 [root@localhost home]# pwd 3 /home 4 5 # 建立/home/pgsql/pgsql_data 6 [root@localhost home]# mkdir pgsql/pgsql_data 7 8 # 賦予pgsql_data文件夾 postgres用戶權限 9 [root@localhost home]# chown postgres:postgres pgsql/pgsql_data
4.初始化數據庫併發
切換到postgres用戶來操做數據庫,pgsql數據庫就以postgres爲默認用戶,執行: su - postgres 切換tcp
1 # 切換到postgres用戶 2 [root@localhost home]# su postgres 3 4 -bash-4.2$ pwd 5 /home 6 7 # 初始化數據庫 8 -bash-4.2$ /home/pgsql/bin/initdb -D /home/pgsql/pgsql_data 9 The files belonging to this database system will be owned by user "postgres". 10 This user must also own the server process. 11 12 The database cluster will be initialized with locale "zh_CN.UTF-8". 13 The default database encoding has accordingly been set to "UTF8". 14 initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8" 15 The default text search configuration will be set to "simple". 16 17 Data page checksums are disabled. 18 19 fixing permissions on existing directory /home/pgsql/pgsql_data ... ok 20 creating subdirectories ... ok 21 selecting default max_connections ... 100 22 selecting default shared_buffers ... 128MB 23 selecting default timezone ... Asia/Shanghai 24 selecting dynamic shared memory implementation ... posix 25 creating configuration files ... ok 26 running bootstrap script ... ok 27 performing post-bootstrap initialization ... ok 28 syncing data to disk ... ok 29 30 WARNING: enabling "trust" authentication for local connections 31 You can change this by editing pg_hba.conf or using the option -A, or 32 --auth-local and --auth-host, the next time you run initdb. 33 34 Success. You can now start the database server using: 35 36 /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l logfile start
5.啓動數據庫ide
初始化數據庫時,最後打印出啓動數據庫命令: /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l logfile start函數
這裏-l指定日誌文件位置,這裏直接輸出在家目錄下的logfile中,這個能夠本身指定,這裏-D指定數據目錄,默認若是不加數據目錄直接報錯找不到,能夠剛纔說的環境變量配置文件中~/.bash_profile加入一行: export PGDATA=/home/pgsql/pgsql_data 而後source進去便可,這樣pgsql會自動去找PGDATA環境變量值,找不到纔會報錯
啓動以後此時執行: ps -ef | grep postgres 就能夠看到相關進程以下:
1 -bash-4.2$ /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l /home/pgsql/logfile start 2 waiting for server to start.... done 3 server started
# 查看postgres進程 4 -bash-4.2$ ps -ef | grep postgres 5 root 20480 4206 0 16:10 pts/0 00:00:00 su - postgres 6 postgres 20481 20480 0 16:10 pts/0 00:00:00 -bash 7 postgres 20544 1 0 16:16 pts/0 00:00:00 /home/pgsql/bin/postgres -D /home/pgsql/pgsql_data 8 postgres 20546 20544 0 16:16 ? 00:00:00 postgres: checkpointer process 9 postgres 20547 20544 0 16:16 ? 00:00:00 postgres: writer process 10 postgres 20548 20544 0 16:16 ? 00:00:00 postgres: wal writer process 11 postgres 20549 20544 0 16:16 ? 00:00:00 postgres: autovacuum launcher process 12 postgres 20550 20544 0 16:16 ? 00:00:00 postgres: stats collector process 13 postgres 20551 20544 0 16:16 ? 00:00:00 postgres: bgworker: logical replication launcher 14 postgres 20552 20481 0 16:16 pts/0 00:00:00 ps -ef 15 postgres 20553 20481 0 16:16 pts/0 00:00:00 grep --color=auto postgres
pgsql默認的端口號爲5432
-bash-4.2$ netstat -tunlp | grep 5432 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20544/postgres tcp6 0 0 ::1:5432 :::* LISTEN 20544/postgres
6.開啓遠程訪問
# 找到pgsql_data文件夾 [root@localhost pgsql_data]# pwd /home/pgsql/pgsql_data [root@localhost pgsql_data]# ls base pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf postmaster.opts global pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf postmaster.pid # vi修改postgresql.conf [root@localhost pgsql_data]# vi postgresql.conf
將 listen_addresses 前的#刪掉,而後把localhost改爲 *
重啓便可。
/home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l /home/pgsql/logfile restart
1 # 修改重啓前 127.0.0.1:5432 2 [root@localhost pgsql_data]# netstat -tunlp | grep 5432 3 tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20544/postgres 4 tcp6 0 0 ::1:5432 :::* LISTEN 20544/postgres 5 6 # 修改重啓後 0.0.0.0:5432 7 [root@localhost pgsql_data]# netstat -tunlp | grep 5432 8 tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 20595/postgres 9 tcp6 0 0 :::5432 :::* LISTEN 20595/postgres
7.鏈接數據庫
1 [root@localhost bin]# /home/pgsql/bin/psql -h 127.0.0.1 -d postgres -U postgres -p 5432 2 psql.bin (10.10) 3 Type "help" for help. 4 5 postgres=#
其中-h參數指定服務器地址,默認爲127.0.0.1,默認不指定便可,-d指定鏈接以後選中的數據庫,默認也是postgres,-U指定用戶,默認是當前用戶,-p 指定端口號,默認是"5432"
查看當前數據庫列表
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
默認postgres,template0和1這3個庫是不容許操做的,建立新的數據庫執行: CREATE DATABASE test WITH OWNER=postgres ENCODING='UTF-8'; 這樣就建立好了數據庫test,而後能夠執行命令 \c test 切換當前數據庫爲test,而後執行 \d 能夠查看當前數據庫下的全部表:
# 建立test數據庫
postgres=# CREATE DATABASE test WITH OWNER=postgres ENCODING='UTF-8'; CREATE DATABASE
# 查看全部數據庫列表 postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres test | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | (4 rows)
# 切換到test數據庫 postgres=# \c test You are now connected to database "test" as user "postgres".
# 查看test數據庫的全部表 test=# \d Did not find any relations.
建立一個表
# 建立一個表student test=# CREATE TABLE student ( test(# id integer NOT NULL, test(# name character(32), test(# number char(5), test(# CONSTRAINT student_pkey PRIMARY KEY (id) test(# ); CREATE TABLE # 查看student表結構 test=# \d student Table "public.student" Column | Type | Collation | Nullable | Default --------+---------------+-----------+----------+--------- id | integer | | not null | name | character(32) | | | number | character(5) | | | Indexes: "student_pkey" PRIMARY KEY, btree (id) # 插入student表一條數據 test=# INSERT INTO student (id,name,number) VALUES (1,'張三','1023'); INSERT 0 1 # 查看插入的數據 test=# SELECT * FROM student WHERE id=1; id | name | number ----+------------------------------------+-------- 1 | 張三 | 1023 (1 row) # \q退出pgsql test=# \q [root@localhost bin]#