通過命令行執(zhí)行初始化sql腳本是比較常見的需求,命令行下執(zhí)行如下操作即可:
若是執(zhí)行的命名只是創(chuàng)建用戶,編輯用戶,創(chuàng)建數(shù)據(jù)庫的話可以不指定-d參數(shù)。
psql -U username -d myDataBase -a -f init.sql
如果是遠(yuǎn)程數(shù)據(jù)庫加入-h參數(shù)指定主機地址即可
psql -h host -U username -d myDataBase -a -f init.sql
補充:PostgreSQL操作-psql基本命令
一、建立數(shù)據(jù)庫連接
接入PostgreSQL數(shù)據(jù)庫: psql -h IP地址 -p 端口 -U 數(shù)據(jù)庫名
之后會要求輸入數(shù)據(jù)庫密碼
二、訪問數(shù)據(jù)庫
1、列舉數(shù)據(jù)庫:\l
2、選擇數(shù)據(jù)庫:\c 數(shù)據(jù)庫名
3、查看該某個庫中的所有表:\dt
4、切換數(shù)據(jù)庫:\c interface
5、查看某個庫中的某個表結(jié)構(gòu):\d 表名
6、查看某個庫中某個表的記錄:select * from apps limit 1;
7、顯示字符集:\encoding
8、退出psgl:\q
列出當(dāng)前數(shù)據(jù)庫所有表
\dt
列出表名
SELECT tablename FROM pg_tables;
WHERE tablename NOT LIKE 'pg%'
AND tablename NOT LIKE 'sql_%'
ORDER BY tablename;
列出數(shù)據(jù)庫名
\l
或
SELECT datname FROM pg_database;
切換數(shù)據(jù)庫
\c 數(shù)據(jù)庫名
1、通過命令行查詢
\d 數(shù)據(jù)庫 —— 得到所有表的名字
\d 表名 —— 得到表結(jié)構(gòu)
2、通過SQL語句查詢
"select * from pg_tables"
—— 得到當(dāng)前db中所有表的信息(這里pg_tables是系統(tǒng)視圖)
"select tablename from pg_tables where schemaname='public'"
—— 得到所有用戶自定義表的名字(這里"tablename"字段是表的名字,"schemaname"是schema的名字。用戶自定義的表,如果未經(jīng)特殊處理,默認(rèn)都是放在名為public的schema下)
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\h [NAME] help on syntax of SQL commands, * for all commands
\q quit psql
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S] [PATTERN] list conversions
\dC [PATTERN] list casts
\dd[S] [PATTERN] show comments on objects
\ddp [PATTERN] list default privileges
\dD[S] [PATTERN] list domains
\det[+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dn[S+] [PATTERN] list schemas
\do[S] [PATTERN] list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dE[S+] [PATTERN] list foreign tables
\dx[+] [PATTERN] list extensions
\l[+] list all databases
\sf[+] FUNCNAME show a function's definition
\z [PATTERN] same as \dp
Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset NAME [VALUE] set table output option
(NAME := {format|border|expanded|fieldsep|footer|null|
numericlocale|recordsep|tuples_only|title|tableattr|pager})
\t [on|off] show only rows (currently off)
\T [STRING] set HTML table> tag attributes, or unset if none
\x [on|off] toggle expanded output (currently off)
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "postgres")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo display information about current connection
Operating System
\cd [DIR] change the current working directory
\timing [on|off] toggle timing of commands (currently off)
\!! [COMMAND] execute command in shell or start interactive shell
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations
postgresql數(shù)據(jù)管理系統(tǒng)使用命令方式有兩種:
1. 內(nèi)部命令,以反斜線開始 \ ,如: \l 顯示所有數(shù)據(jù)庫
2. 標(biāo)準(zhǔn)SQL命令,以分號 ; 或 \g 結(jié)束,可以使用多行
數(shù)據(jù)庫的關(guān)鍵操作:
1. 啟動服務(wù) 2. 登錄 3. 建立數(shù)據(jù)庫 4. 建立表 5. 插入記錄到表中
6. 更新/刪除/查詢/修改操作 7. 退出 8. 停止服務(wù)
在windows7中安裝的postgresql默認(rèn)使用GBK字符集,經(jīng)常不能使用顯示中文的數(shù)據(jù)表,解決辦法:
注意:在windows 7下的postgresql中寫操作時要使用GBK,讀操作時要用UTF8;
設(shè)置字符集為 utf-8 就可以了.
postgres=# \encoding utf-8 // 設(shè)置客戶端的字元集
postgres=# \encoding // 顯示客戶端的字元集
postgres=# show client_encoding; // 顯示客戶端的字元集
postgres=# show server_encoding; // 顯示服務(wù)器的字元集
啟動服務(wù):
停止服務(wù):
獲取命令幫助:
登錄( 注意: postgres 是默認(rèn)用戶即管理員 ):
路徑 psql -h 服務(wù)器 -U 用戶名 -d 數(shù)據(jù)庫 -p 端口地址 // -U 是大寫
C:\&; psql -h localhost -U postgres -p 5432 // 默認(rèn)打開postgres數(shù)據(jù)庫
C:\&; psql -h 127.0.0.1 -U postgres -d fengdos -p 5432 // 打開fengdos數(shù)據(jù)庫
C:\&; psql -U postgres // 快速登錄(全部使用默認(rèn)設(shè)置)
// 使用某些有密碼的用戶的情況下, 會提示輸入密碼.
用戶 postgres 的口令: ILoveYou // 輸入時不會顯示任何字符
// 成功后顯示:
psql (9.5.3)
輸入 "help" 來獲取幫助信息.
// 進(jìn)入postgresql數(shù)據(jù)庫系統(tǒng)提示符狀態(tài), ******=# 中=#前面為當(dāng)前使用的數(shù)據(jù)庫
postgres=# help // 獲取系統(tǒng)幫助,顯示如下:
您正在使用psql, 這是一種用于訪問PostgreSQL的命令行界面
鍵入:
\copyright
顯示發(fā)行條款
\h
顯示 SQL 命令的說明
\&;
顯示 pgsql 命令的說明 (pgsql內(nèi)部命令)
\g
或者以分號(;)結(jié)尾以執(zhí)行查詢
\q
退出注: 數(shù)據(jù)庫名稱區(qū)分大小寫的。
postgres=# \help // 獲取SQL命令的幫助,同 \h
postgres=# \quit // 退出,同 \q
postgres=# \password dlf // 重新設(shè)置用戶dlf的密碼,然后需要 \q退出后才生效
c:\&;psql exampledb user.sql // 將user.sql文件導(dǎo)入到exampled數(shù)據(jù)庫中
postgres=# \h select // 精細(xì)顯示SQL命令中的select命令的使用方法
postgres=# \l // 顯示所有數(shù)據(jù)庫
postgres=# \dt // 顯示當(dāng)前數(shù)據(jù)庫中的所有表
postgres=# \d [table_name] // 顯示當(dāng)前數(shù)據(jù)庫的指定表的表結(jié)構(gòu)
postgres=# \c [database_name] // 切換到指定數(shù)據(jù)庫,相當(dāng)于use
postgres=# \du // 顯示所有用戶
postgres=# \conninfo // 顯示當(dāng)前數(shù)據(jù)庫和連接信息
postgres=# \e // 進(jìn)入記事本sql腳本編輯狀態(tài)(輸入批命令后關(guān)閉將自動在命令行中執(zhí)行)
postgres=# \di // 查看索引(要建立關(guān)聯(lián))
postgres=# \prompt [文本] 名稱 // 提示用戶設(shè)定內(nèi)部變數(shù)
postgres=# \encoding [字元編碼名稱] // 顯示或設(shè)定用戶端字元編碼
*可以將存儲過程寫在文本文件中aaa.sql,然后在psql狀態(tài)下:
postgres=# \i aaa.sql // 將aaa.sql導(dǎo)入(到當(dāng)前數(shù)據(jù)庫)
postgres=# \df // 查看所有存儲過程(函數(shù))
postgres=# \df+ name // 查看某一存儲過程
postgres=# select version(); // 獲取版本信息
postgres=# select usename from pg_user; // 獲取系統(tǒng)用戶信息
postgres=# drop User 用戶名 // 刪除用戶
其它SQL命令通用如(標(biāo)準(zhǔn)化SQL語句):
*創(chuàng)建數(shù)據(jù)庫:
create database [數(shù)據(jù)庫名];
*刪除數(shù)據(jù)庫:
drop database [數(shù)據(jù)庫名];
*創(chuàng)建表:
create table ([字段名1] [類型1] ;,[字段名2] [類型2],......,primary key (字段名m,字段名n,...)>;);
*在表中插入數(shù)據(jù):
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
*顯示表內(nèi)容:
*重命名一個表:
alter table [表名A] rename to [表名B];
*刪除一個表:
*在已有的表里添加字段:
alter table [表名] add column [字段名] [類型];
*刪除表中的字段:
alter table [表名] drop column [字段名];
*重命名一個字段:
alter table [表名] rename column [字段名A] to [字段名B];
*給一個字段設(shè)置缺省值:
alter table [表名] alter column [字段名] set default [新的默認(rèn)值];
*去除缺省值:
alter table [表名] alter column [字段名] drop default;
*修改表中的某行某列的數(shù)據(jù):
update [表名] set [目標(biāo)字段名]=[目標(biāo)值] where [該行特征];
*刪除表中某行數(shù)據(jù):
delete from [表名] where [該行特征];
delete from [表名]; // 刪空整個表
*可以使用pg_dump和pg_dumpall來完成。比如備份sales數(shù)據(jù)庫:
pg_dump drupal>/opt/Postgresql/backup/1.bak
1.列出所有表名的查詢語句
SELECT tablename FROM pg_tables
WHERE tablename NOT LIKE 'pg%'
AND tablename NOT LIKE 'sql_%'
ORDER BY tablename;
2.列出表中所有的數(shù)據(jù)
3.執(zhí)行外部腳本
#/opt/PostgreSQL/8. 3/bin/psql - Upostgres ;登陸到數(shù)據(jù)庫的控制臺界面
postgres= # \i /root/db. sql
\i 命令用于執(zhí)行一個外部的sql腳本文件。
4.導(dǎo)出數(shù)據(jù)庫為外部的腳本
#/opt/PostgreSQL/8. 3/bin/ pg_dump - Upostgres - C - fdb. sql database
-C create -f 是導(dǎo)出后的文件名
5.postgresql 插入16進(jìn)制數(shù)
INSERT INTO tableAAA VALUES( x'0001f' : : integer, '鑒權(quán)' , 'Authority' )
6.使用 TG_RELNAME 報錯ERROR: syntax error at or near "$1" at character
[引]http://www.dbmonster.com/Uwe/Forum.aspx/postgresql/2051/TG-RELNAME-problem
Perhaps you will get some idea if you read the document:
37. 6. 4. Executing Dynamic Commands
改:執(zhí)行動態(tài)語句
EXECUTE 'INSERT INTO TG_RELNAME VALUES (NEW.start_time , NEW.id , NEW.end_time)';
7. psql 常用命令
a. \c tesdb1 - - 將當(dāng)前連接的testdb數(shù)據(jù)庫改變成 testdb1 。
b . \q - - 斷開與Postgres服務(wù)器的連接
c . \l 列出所有數(shù)據(jù)庫的名字
\l+ 列出所有數(shù)據(jù)庫的名字以及字符集編碼
d. \d [ 名字] 描述表, 索引, 序列, 或者視圖
列出表/索引/序列/視圖/系統(tǒng)表
\d{t| i| s| v| S} [ 模式] ( 加 "+" 獲取更多信息)
- - 列出表/索引/序列/視圖/系統(tǒng)表
\d tablename - - 查看表的結(jié)構(gòu)
\dt - - 列出數(shù)據(jù)庫中所有表
8.在PostgreSQL中如何刪除重復(fù)記錄
在PostgreSQL中刪除重復(fù)記錄其實很簡單,不論有多少行重復(fù),只要在要刪除重復(fù)記錄的表中table加一列rownum字段( id為table表中的主鍵) ,類型設(shè)置為serial類型即可,然后執(zhí)行sql
delete from deltest where rownum not in(
select max(rownum) from deltest
);
最后刪除列rownum即可
正文:
連接數(shù)據(jù)庫操作:
psql是postgresql數(shù)據(jù)庫提供的連接數(shù)據(jù)庫shell命令,格式 psql 【option】 dbname
在終端輸入psql 會使用默認(rèn)的方式連接本地數(shù)據(jù)庫,使用的用戶名是登陸linux系統(tǒng)使用的用戶名,
psql -U username -W pass 以及psql -U username -W pass databasenaem都可以實現(xiàn)連接數(shù)據(jù)庫的功能,第一種方式是使用用戶名username密碼pass連接默認(rèn)數(shù)據(jù)庫(具體鏈接那個數(shù)據(jù)庫還沒搞清 楚),第二種方式使用用戶名username密碼pass連接username數(shù)據(jù)庫。如果登錄成功之后將顯示類似信息
Welcome to psql 8.0.6, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\&; for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
連接成功之后所有的命令都是使用”\“+ 字符或者word完成相應(yīng)的功能。現(xiàn)將常用的幾個列車
\l
列出所有數(shù)據(jù)庫
\dt
列出連接數(shù)據(jù)庫中所有表
\di
列出連接數(shù)據(jù)庫中所有index
\dv
列出連接數(shù)據(jù)庫中所有view
\h
sql命令幫助
\&;
\ 所有命令幫助
\q
退出連接
\d tablename
列出指定tablename的表結(jié)構(gòu)
可以嘗試執(zhí)行下面兩句sql
SELECT current_date
SELECT version()
是不是nothing happened,這是因為postgresql數(shù)據(jù)庫要求必須使用;結(jié)尾否則不予執(zhí)行,加上;之后就能看到結(jié)果了。
如果我們想創(chuàng)建數(shù)據(jù)庫怎么辦呢?
我們知道createdb和dropdb可以創(chuàng)建和刪除數(shù)據(jù)庫,但是如果我們這個時候執(zhí)行出現(xiàn)什么問題呢?可以試一試,提示是個錯誤。
為什么呢?
createdb和dropdb是shell腳本,所以現(xiàn)在又兩種方式執(zhí)行
(1).退出連接進(jìn)入終端,輸入createdb test —U user -W pass 稍等提示創(chuàng)建數(shù)據(jù)庫成功
dropdb test —U user -W pass 提示drop成功
(2).在未退出連接中使用 \!! createdb test —U user -W pass 稍等提示創(chuàng)建數(shù)據(jù)庫成功
\!! dropdb test —U user -W pass 提示drop成功
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- PostgreSQL查看正在執(zhí)行的任務(wù)并強制結(jié)束的操作方法
- 解決PostgreSQL 執(zhí)行超時的情況
- PostgreSQL 實現(xiàn)定時job執(zhí)行(pgAgent)
- Postgresql的pl/pgql使用操作--將多條執(zhí)行語句作為一個事務(wù)
- Postgresql psql文件執(zhí)行與批處理多個sql文件操作
- 在postgresql中結(jié)束掉正在執(zhí)行的SQL語句操作
- PostgreSQL 實現(xiàn)sql放入文件批量執(zhí)行