网站介绍怎么写范文,做推广都有什么网站,wordpress全站静态化,网页图片不能保存怎么破解psql 客户端使用 专栏内容#xff1a; postgresql内核源码分析手写数据库toadb并发编程 开源贡献#xff1a; toadb开源库 个人主页#xff1a;我的主页 管理社区#xff1a;开源数据库 座右铭#xff1a;天行健#xff0c;君子以自强不息#xff1b;地势坤#xf…psql 客户端使用 专栏内容 postgresql内核源码分析手写数据库toadb并发编程 开源贡献 toadb开源库 个人主页我的主页 管理社区开源数据库 座右铭天行健君子以自强不息地势坤君子以厚德载物. 系列文章
初始化集群数据库服务管理psql客户端使用
前言
postgresql 数据库是一款通用的关系型数据在开源数据库中能与商业数据媲美在业界也越来越流行。
因为是开源数据库不仅公开源码还有很多使用案例好用的插件所以它的慢慢变成了数据库的先驱和标准通过postgresql可以很好从使用到原理彻底搞懂
如果是学习编程也可以学到丰富的编程知识数据结构编程技巧它里面还有很多精妙的架构设计分层思想可以灵活定制的思想。
本专栏主要介绍postgresql 入门使用数据库维护管理通过这些使用来了解数据库原理慢慢了解postgresql是什么样的数据库能做那些事情以及如何做好服务最关键的是这些知识都是面试的必备项。
概述
数据库服务在后台运行除了开发应用程序通过jdbc,odbc等与它交互外如何能操作数据库呢
像oracle,mysql一样postgresql也有自己的客户端可以很方便数据库开发人员看到数据库中的结构查询数据库中的数据尤其像图形化客户端帮助我们减少SQL编程通过图形化选择就可以轻松搞定数据库的设计
当然也有命令行的客户端相对图形化界面来讲很轻量也可以被其它应用嵌入使用但是需要对数据库有一定了解对数据库结构了然于胸其实用习惯了命令行反而觉得界面比较麻烦。
本文主要分享命令行客户端的常用命令和方法
命令行客户端
客户端当然离不开命令行方式因为在大多数linux下都是使用命令行;
postgresql 在安装时就自带了一个功能强大的命令行客户端工具 psql 它就在安装目录的bin目录下面
命令说明
先来看一下psql自身的帮助
[senllanghatch bin]$ ./psql --help
psql is the PostgreSQL interactive terminal.Usage:psql [OPTION]... [DBNAME [USERNAME]]General options:-c, --commandCOMMAND run only single command (SQL or internal) and exit-d, --dbnameDBNAME database name to connect to (default: senllang)-f, --fileFILENAME execute commands from file, then exit-l, --list list available databases, then exit-v, --set, --variableNAMEVALUEset psql variable NAME to VALUE(e.g., -v ON_ERROR_STOP1)-V, --version output version information, then exit-X, --no-psqlrc do not read startup file (~/.psqlrc)-1 (one), --single-transactionexecute as a single transaction (if non-interactive)-?, --help[options] show this help, then exit--helpcommands list backslash commands, then exit--helpvariables list special variables, then exitInput and output options:-a, --echo-all echo all input from script-b, --echo-errors echo failed commands-e, --echo-queries echo commands sent to server-E, --echo-hidden display queries that internal commands generate-L, --log-fileFILENAME send session log to file-n, --no-readline disable enhanced command line editing (readline)-o, --outputFILENAME send query results to file (or |pipe)-q, --quiet run quietly (no messages, only query output)-s, --single-step single-step mode (confirm each query)-S, --single-line single-line mode (end of line terminates SQL command)Output format options:-A, --no-align unaligned table output mode--csv CSV (Comma-Separated Values) table output mode-F, --field-separatorSTRINGfield separator for unaligned output (default: |)-H, --html HTML table output mode-P, --psetVAR[ARG] set printing option VAR to ARG (see \pset command)-R, --record-separatorSTRINGrecord separator for unaligned output (default: newline)-t, --tuples-only print rows only-T, --table-attrTEXT set HTML table tag attributes (e.g., width, border)-x, --expanded turn on expanded table output-z, --field-separator-zeroset field separator for unaligned output to zero byte-0, --record-separator-zeroset record separator for unaligned output to zero byteConnection options:-h, --hostHOSTNAME database server host or socket directory (default: local socket)-p, --portPORT database server port (default: 5432)-U, --usernameUSERNAME database user name (default: senllang)-w, --no-password never prompt for password-W, --password force password prompt (should happen automatically)For more information, type \? (for internal commands) or \help (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.Report bugs to pgsql-bugslists.postgresql.org.
PostgreSQL home page: https://www.postgresql.org/使用说明
有几个常用的参数在这里列一下
一般登陆使用的参数
-d database name , 指定要连接的数据库的名字每个连接都需要指定到数据库-U username, 登陆数据库要使用的数据库用户名-h hostname , 指定数据库服务的IP或如果为localhost可以不指定-p port 数据库服务的port端口号默认5432时也不需要指定 默认数据库名和用户名是一样的 同当前的系统用户名如果当前系统用户名不是postgres那就需要指定数据库名
这里需要注意下因为数据库集群中默认会创建一个postgres的数据库当前初始化时没有指定用户名默认是当有系统用户名
举例说明新初始化的集群启动后用psql进行连接超级用户名为senllang, 数据库为postgres, 主机名为localhost, 端口号为5432;
[senllanghatch bin]$ ./psql -d postgres
psql (16beta1)
Type help for help.
从文件执行SQL的参数
假如我们要从文件导入除了指定上面的参数以外还有
-f file ,指定带路径的文件名
假如我们有一个createtable.sql的文件通过执行文件来创建表文件内容如下
--
create table tbl_user(id int, name varchar, ssex varchar);使用psql执行文件
[senllanghatch bin]$ ./psql -d postgres -f createtable.sql
CREATE TABLE可以看到它执行成功了
直接执行SQL
使用psql 也可以直接执行SQL语句这个功能也非常好用比如在shell中就可以调用psql操作数据库
-c command , 指定要执行的命令用双引号括起来 现在向刚才创建的表中插入一条数据
[senllanghatch bin]$ ./psql -d postgres -c insert into tbl_user values(1,xiucai,male);
INSERT 0 1常用命令
通过psql, 我们连接上数据库后如何使用呢
下面我们介绍几个常用的命令和用法
SQL命令帮助可以执行 \h查看当前数据库下的用户表 \d 会显示出来所属的schema,own 用户名等
postgres# \dList of relationsSchema | Name | Type | Owner
-----------------------------------public | tbl_user | table | senllang
(1 row)查看当前集群中的database列表 \l
postgres# \lList of databasesName | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
------------------------------------------------------------------------------------------------------------------------postgres | senllang | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US | |template0 | senllang | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US | | c/senllang | | | | | | | | senllangCTc/senllangtemplate1 | senllang | UTF8 | icu | en_US.UTF-8 | en_US.UTF-8 | en-US | | c/senllang | | | | | | | | senllangCTc/senllang
(3 rows)可以到已经有3个数据库其中两个template0,template1是模版数据库也就是生成其它数据库的模版而postgres就是默认创建的可以使用的数据库
切换用户或数据库 \c
postgres# create database db_factory;
CREATE DATABASE
postgres# \c db_factory
You are now connected to database db_factory as user senllang.
db_factory#再新创建一个数据库db_factory然后切换到该数据库默认使用当前登陆的用户名
总结
自带的 psql命令行客户端非常实用以上分享了一些它的用法和常用命令其它我们在以后的分享中逐淅再接触。
了解psql之后我们就可以真正的开始数据库使用了以上在演示过程中已经输入了一些SQL命令如果你已经掌握一些SQL那么就可以在psql连接数据库之后开始输入SQL开始你的数据库设计了。
结尾 非常感谢大家的支持在浏览的同时别忘了留下您宝贵的评论如果觉得值得鼓励请点赞收藏我会更加努力 作者邮箱studysenllang.onaliyun.com 如有错误或者疏漏欢迎指出互相学习。
注未经同意不得转载