最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在postgresql中通過命令行執(zhí)行sql文件

 更新時(shí)間:2021年01月06日 09:16:08   作者:淘金者05012  
這篇文章主要介紹了在postgresql中通過命令行執(zhí)行sql文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

通過命令行執(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ù)指定主機(jī)地址即可

psql -h host -U username -d myDataBase -a -f init.sql

補(bǔ)充:PostgreSQL操作-psql基本命令

一、建立數(shù)據(jù)庫連接

接入PostgreSQL數(shù)據(jù)庫: psql -h IP地址 -p 端口 -U 數(shù)據(jù)庫名

之后會(huì)要求輸入數(shù)據(jù)庫密碼

二、訪問數(shù)據(jù)庫

1、列舉數(shù)據(jù)庫:\l

2、選擇數(shù)據(jù)庫:\c 數(shù)據(jù)庫名

3、查看該某個(gè)庫中的所有表:\dt

4、切換數(shù)據(jù)庫:\c interface

5、查看某個(gè)庫中的某個(gè)表結(jié)構(gòu):\d 表名

6、查看某個(gè)庫中某個(gè)表的記錄: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命令,以分號(hào) ; 或 \g 結(jié)束,可以使用多行

數(shù)據(jù)庫的關(guān)鍵操作:

1. 啟動(dòng)服務(wù) 2. 登錄 3. 建立數(shù)據(jù)庫 4. 建立表 5. 插入記錄到表中

6. 更新/刪除/查詢/修改操作 7. 退出 8. 停止服務(wù)

在windows7中安裝的postgresql默認(rèn)使用GBK字符集,經(jīng)常不能使用顯示中文的數(shù)據(jù)表,解決辦法:

注意:在windows 7下的postgresql中寫操作時(shí)要使用GBK,讀操作時(shí)要用UTF8;

設(shè)置字符集為 utf-8 就可以了.

postgres=# \encoding utf-8  // 設(shè)置客戶端的字元集
postgres=# \encoding     // 顯示客戶端的字元集
postgres=# show client_encoding;  // 顯示客戶端的字元集
postgres=# show server_encoding;  // 顯示服務(wù)器的字元集

啟動(dòng)服務(wù):

net start postgresql-9.5

停止服務(wù):

net stop postgresql-9.5

獲取命令幫助:

c:\> psql --help

登錄( 注意: 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è)置)
// 使用某些有密碼的用戶的情況下, 會(huì)提示輸入密碼.
用戶 postgres 的口令: ILoveYou   // 輸入時(shí)不會(huì)顯示任何字符
// 成功后顯示:
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 或者以分號(hào)(;)結(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)閉將自動(dòng)在命令行中執(zhí)行)
postgres=# \di // 查看索引(要建立關(guān)聯(lián))
postgres=# \prompt [文本] 名稱 // 提示用戶設(shè)定內(nèi)部變數(shù)
postgres=# \encoding [字元編碼名稱] // 顯示或設(shè)定用戶端字元編碼
*可以將存儲(chǔ)過程寫在文本文件中aaa.sql,然后在psql狀態(tài)下:
postgres=# \i aaa.sql // 將aaa.sql導(dǎo)入(到當(dāng)前數(shù)據(jù)庫)
postgres=# \df   // 查看所有存儲(chǔ)過程(函數(shù))
postgres=# \df+ name  // 查看某一存儲(chǔ)過程
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)容:

select * from student;

*重命名一個(gè)表:

alter table [表名A] rename to [表名B];

*刪除一個(gè)表:

drop table [表名]; 

*在已有的表里添加字段:

alter table [表名] add column [字段名] [類型];

*刪除表中的字段:

alter table [表名] drop column [字段名];

*重命名一個(gè)字段:

alter table [表名] rename column [字段名A] to [字段名B];

*給一個(gè)字段設(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 [表名]; // 刪空整個(gè)表

*可以使用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ù)

SELECT * FROM someTable;

3.執(zhí)行外部腳本

#/opt/PostgreSQL/8. 3/bin/psql - Upostgres ;登陸到數(shù)據(jù)庫的控制臺(tái)界面
postgres= # \i /root/db. sql
  \i 命令用于執(zhí)行一個(gè)外部的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 報(bào)錯(cuò)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í)行動(dòng)態(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ù)記錄其實(shí)很簡(jiǎn)單,不論有多少行重復(fù),只要在要?jiǎng)h除重復(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 會(huì)使用默認(rèn)的方式連接本地?cái)?shù)據(jù)庫,使用的用戶名是登陸linux系統(tǒng)使用的用戶名,

psql -U username -W pass 以及psql -U username -W pass databasenaem都可以實(shí)現(xiàn)連接數(shù)據(jù)庫的功能,第一種方式是使用用戶名username密碼pass連接默認(rèn)數(shù)據(jù)庫(具體鏈接那個(gè)數(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)的功能?,F(xiàn)將常用的幾個(gè)列車

\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,這是因?yàn)閜ostgresql數(shù)據(jù)庫要求必須使用;結(jié)尾否則不予執(zhí)行,加上;之后就能看到結(jié)果了。

如果我們想創(chuàng)建數(shù)據(jù)庫怎么辦呢?

我們知道createdb和dropdb可以創(chuàng)建和刪除數(shù)據(jù)庫,但是如果我們這個(gè)時(shí)候執(zhí)行出現(xiàn)什么問題呢?可以試一試,提示是個(gè)錯(cuò)誤。

為什么呢?

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成功

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 詳解如何在Ubuntu?18.04上安裝和使用PostgreSQL

    詳解如何在Ubuntu?18.04上安裝和使用PostgreSQL

    關(guān)系數(shù)據(jù)庫管理系統(tǒng)是許多網(wǎng)站和應(yīng)用程序的關(guān)鍵組件,它們提供了一種結(jié)構(gòu)化的方式來存儲(chǔ)、組織和訪問信息,本文演示了如何在?Ubuntu?18.04?VPS?實(shí)例上安裝?Postgres,并提供了基本數(shù)據(jù)庫管理的說明,需要的朋友可以參考下
    2024-07-07
  • Windows下Postgresql下載與配置方法

    Windows下Postgresql下載與配置方法

    這篇文章主要為大家詳細(xì)介紹了Windows下Postgresql下載與配置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • postgresql修改完端口后直接psql連接數(shù)據(jù)庫報(bào)錯(cuò)的解決

    postgresql修改完端口后直接psql連接數(shù)據(jù)庫報(bào)錯(cuò)的解決

    這篇文章主要介紹了postgresql修改完端口后直接psql連接數(shù)據(jù)庫報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 基于Postgresql 事務(wù)的提交與回滾解析

    基于Postgresql 事務(wù)的提交與回滾解析

    這篇文章主要介紹了基于Postgresql 事務(wù)的提交與回滾解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSql觸發(fā)器創(chuàng)建、使用以及刪除示例詳解

    PostgreSql觸發(fā)器創(chuàng)建、使用以及刪除示例詳解

    在PostgreSQL中觸發(fā)器函數(shù)是一個(gè)沒有參數(shù)并且返回trigger類型的函數(shù),這篇文章主要給大家介紹了關(guān)于PostgreSql觸發(fā)器創(chuàng)建、使用以及刪除的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • PostgreSQL 默認(rèn)權(quán)限查看方式

    PostgreSQL 默認(rèn)權(quán)限查看方式

    這篇文章主要介紹了PostgreSQL 默認(rèn)權(quán)限查看方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL常用字符串函數(shù)與示例說明小結(jié)

    PostgreSQL常用字符串函數(shù)與示例說明小結(jié)

    文章介紹了PostgreSQL中常用字符串函數(shù)的使用方法,包括空值處理、字符串位置查詢、長度計(jì)算、大小寫轉(zhuǎn)換、去除空格、連接、替換、匹配、拆分和截取等操作,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令小結(jié)

    PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令小結(jié)

    這篇文章主要給大家介紹了PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令操作,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-12-12
  • shell腳本操作postgresql的方法

    shell腳本操作postgresql的方法

    PostgreSQL支持大部分的SQL標(biāo)準(zhǔn)并且提供了很多其他現(xiàn)代特性,如復(fù)雜查詢、外鍵、觸發(fā)器、視圖、事務(wù)完整性、多版本并發(fā)控制等這篇文章主要介紹了shell腳本操作postgresql,需要的朋友可以參考下
    2022-12-12
  • PostgreSQL建立自增主鍵的2種方法總結(jié)

    PostgreSQL建立自增主鍵的2種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于PostgreSQL建立自增主鍵的2種方法,PostgreSQL主鍵自增是一種自動(dòng)增長的機(jī)制,可以為表中的每一行記錄分配唯一的標(biāo)識(shí)符,需要的朋友可以參考下
    2023-09-09

最新評(píng)論

朝阳县| 卓尼县| 鹤庆县| 新源县| 慈溪市| 金塔县| 南皮县| 太湖县| 南京市| 东至县| 巴塘县| 克拉玛依市| 中超| 卢湾区| 东乡| 黄冈市| 韶关市| 海淀区| 岗巴县| 遂宁市| 且末县| 调兵山市| 呼玛县| 临沧市| 余江县| 淳安县| 龙胜| 长治县| 平江县| 贞丰县| 治县。| 三门峡市| 那坡县| 潢川县| 辽中县| 密山市| 报价| 兖州市| 汝州市| 哈巴河县| 富锦市|