正文
关于在命令行中运行postgresql的信息
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
linux怎么用命令登陆postgres
(1)用户实用程序:
createdb 创建一个新的PostgreSQL的数据库(和SQL语句:CREATE DATABASE 相同)
createuser 创建一个新的PostgreSQL的用户(和SQL语句:CREATE USER 相同)
dropdb 删除数据库
dropuser 删除用户
pg_dump 将PostgreSQL数据库导出到一个脚本文件
pg_dumpall 将所有的PostgreSQL数据库导出到一个脚本文件
pg_restore 从一个由pg_dump或pg_dumpall程序导出的脚本文件中恢复PostgreSQL数据库
psql 一个基于命令行的PostgreSQL交互式客户端程序
vacuumdb 清理和分析一个PostgreSQL数据库,它是客户端程序psql环境下SQL语句VACUUM的shell脚本封装,二者功能完全相同
(2)系统实用程序
initdb 创建一个用于存储数据库的PostgreSQL数据目录,并创建预定义的模板数据库template0和template1,生成共享目录表 catalog;此程序通常只在安装PostgreSQL时运行一次
initlocation 创建一个辅助的PostgreSQL数据库存储区域
ipcclean 从停止的PostgreSQL服务器中清除共享内在和孤立信号标志
pg_ctl 启动、停止、重启PostgreSQL服务(比如:pg_ctl start 启动PostgreSQL服务,它和service postgresql start相同)
pg_controldata 显示PostgreSQL服务的内部控制信息
postgres PostgreSQL单用户模式的数据库服务
postmaster PostgreSQL多用户模式的数据库服务
4.这里面最重要的是psql这个客户端程序最为重要。启用客户端程序psql的方法是:
切换到PostgreSQL预定义的数据库超级用户postgres,启用客户端程序psql,并连接到自己想要的数据库,比如说:
psql template1
出现以下界面,说明已经进入到想要的数据库,可以进行想要的操作了。
template1=#
5.在数据库中的一些命令:
template1=# \l 查看系统中现存的数据库
template1=# \q 退出客户端程序psql
template1=# \c 从一个数据库中转到另一个数据库中,如template1=# \c sales 从template1转到sales
template1=# \dt 查看表
template1=# \d 查看表结构
template1=# \di 查看索引
[基本数据库操作]========================
*创建数据库:
create database [数据库名];
*查看数据库列表:
\d
*删除数据库:
drop database [数据库名];
创建表:
create table ([字段名1] [类型1] references 关联表名(关联的字段名);,[字段名2] [类型2],......,primary key (字段名m,字段名n,...););
*查看表名列表:
\d
*查看某个表的状况:
\d [表名]
*重命名一个表:
alter table [表名A] rename to [表名B];
*删除一个表:
drop table [表名];
========================================
[表内基本操作]==========================
*在已有的表里添加字段:
alter table [表名] add column [字段名] [类型];
*删除表中的字段:
alter table [表名] drop column [字段名];
*重命名一个字段:
alter table [表名] rename column [字段名A] to [字段名B];
*给一个字段设置缺省值:
alter table [表名] alter column [字段名] set default [新的默认值];
*去除缺省值:
alter table [表名] alter column [字段名] drop default;
在表中插入数据:
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
修改表中的某行某列的数据:
update [表名] set [目标字段名]=[目标值] where [该行特征];
删除表中某行数据:
delete from [表名] where [该行特征];
delete from [表名];--删空整个表
6.要注意随时对数据库进行清理、收回磁盘空间并更新统计信息,使用下面的命令就搞定!
vaccumdb -d sales -z
-a 对所有的数据库操作
-z 保证不断地删除失效的行,节约磁盘空间,将统计信息更新为最近的状态
7.PostgreSQL用户认证
PostgreSQL数据目录中的pg_hba.conf的作用就是用户认证,可以在/var/lib/pgsql/data中找到。
有以下几个例子可以看看:
(1)允许在本机上的任何身份连接任何数据库
TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
local all all trust(无条件进行连接)
(2)允许IP地址为192.168.1.x的任何主机与数据库sales连接
TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host sales all 192.168.1.0 255.255.255.0 ident sameuser(表明任何操作系统用户都能够以同名数据库用户进行连接)
8.看了那么多,来一个完整的创建PostgreSQL数据库用户的示例吧
(1)进入PostgreSQL高级用户
(2)启用客户端程序,并进入template1数据库
psql template1
(3)创建用户
template1=# CREATE USER hellen WITH ENCRYPED PASSWORD'zhenzhen'
(4)因为设置了密码,所以要编辑pg_hba.conf,使用户和配置文件同步。
在原有记录上面添加md5
local all hellen md5
(4)使用新用户登录数据库
template1=# \q
psql -U hellen -d template1
PS:在一个数据库中如果要切换用户,要使用如下命令:
template1=# \!psql -U tk -d template1
9.设定用户特定的权限
还是要用例子来说明:
创建一个用户组:
sales=# CREATE GROUP sale;
添加几个用户进入该组
sales=# ALTER GROUP sale ADD USER sale1,sale2,sale3;
授予用户级sale针对表employee和products的SELECT权限
sales=# GRANT SELECT ON employee,products TO GROUP sale;
在sale中将用户user2删除
sales=# ALTER GROP sale DROP USER sale2;
10.备份数据库
可以使用pg_dump和pg_dumpall来完成。比如备份sales数据库:
pg_dump sales/home/tk/pgsql/backup/1.bak
如何用命令打开postgresql数据库
F:\PostgreSQL\9.2\binpsql.exe
-h
localhost
-U
postgres
-d
Test
-p
5432psql
(9.2.4)输入
"help"
来获取帮助信息.Test=#Test=#
help;您正在使用psql,
这是一种用于访问PostgreSQL的命令行界面键入:
\copyright
显示发行条款
\h
显示
SQL
命令的说明
\?
显示
pgsql
命令的说明
\g
或者以分号(;)结尾以执行查询
\q
退出注:
数据库名称区分大小写的。使用某些有密码的用户的情况下,
会提示输入密码.F:\PostgreSQL\9.2\binpsql.exe
-h
localhost
-U
test
-d
Test
-p
5432用户
test
的口令:psql
(9.2.4)输入
"help"
来获取帮助信息.Test=#
postgresql 数据库问题
错误代码中已经提示了使用perform来替代,说明你的select语句,就必须写到一个变量中。
$$
declare v_cmd text;
begin
v_cmd='select count(*) from "user" where username=$1 and password=$2';
PERFORM exec_shell(v_cmd);
按照这个去修改,这样就可以了,同时建立一个exec_shell()的函数。
CREATE OR REPLACE FUNCTION exec_shell(character varying)
RETURNS integer AS
$BODY$
system($_[0]);
return 1;
$BODY$
LANGUAGE plperlu VOLATILE
COST 1;
postgresql数据库用什么命令执行脚本文件
在命令行下执行:psql -d 库名 -f 文件名;
也可直接在sql脚本开头加:psql 库名 (-U 用户名)!
然后就可以直接运行这个脚本,sh 之或直接赋予可执行权限。
trampwind(随风) 于 2005-3-10 11:42:27
在psql交互界面中可以直接用: i 文件名;
来执行SQL脚本
先把要执行的命令放到 .sql 格式的文件中 ,比如 gcz_test.sql
然后执行 ./gcz_test.sql。
Windows系统中有哪些方法开启Postgresql服务
我们在使用Postgresql的时候,首先需要开启Postgresql服务。那么在Windows系统中有哪些方法开启Postgresql服务呢?下面我给大家分享一下。
工具/材料
任务管理器,服务面板,CMD命令行
服务面板开启 01
首先按Win+R组合键打开运行窗口,在输入框中输入services.msc命令,如下图所示
02
在出现的服务界面中我们找到postgresql的记录行,如下图所示,右键单击选择启动
03
当服务的状态显示为正在运行的时候则代表postgresql服务已经打开了,如下图所示
任务管理器开启 01
按住键盘上的Ctrl+Shift+ESC组合键,注意是同时按下,不是一个个按,如下图所示
02
在弹出的任务管理器窗口中切换到服务面板,找到postgresql服务,右键单击选择开始选项,如下图所示
命令行开启 01
找到所有程序下面的Windows系统,然后点击命令提示符,如下图所示
02
另外也可以直接打开运行界面输入CMD命令打开命令提示符界面,如下图所示
03
在命令提示符中通过net start命令来开启postgresql服务,如下图所示,注意服务名一定要写正确
关于在命令行中运行postgresql和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。