postgresql常用命令
postgresql常用命令 (1)用户实用程序: createdb 创建一个新的PostgreSQL的数据库(和SQL语句:CREATE DATABASE 相同) createuser 创建一个新的PostgreSQL的用户(和SQL语句:CREATE USER 相同) dropdb 删除数据库 dropuser 删除用户 pg_dump 将PostgreS
postgresql常用命令
(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)系统实用程序
1. pg_ctl 启动、停止、重启PostgreSQL服务(比如:pg_ctl start 启动PostgreSQL服务,它和service postgresql start相同)
2. pg_controldata 显示PostgreSQL服务的内部控制信息
3. psql 切换到PostgreSQL预定义的数据库超级用户postgres,启用客户端程序psql,并连接到自己想要的数据库,比如说:
psql template1
出现以下界面,说明已经进入到想要的数据库,可以进行想要的操作了。
template1=#
(3).在数据库中的一些命令:
template1=# \l 查看系统中现存的数据库
template1=# \q 退出客户端程序psql
template1=# \c 从一个数据库中转到另一个数据库中,如template1=# \c sales 从template1转到sales
template1=# \dt 查看表
template1=# \d 查看表结构
template1=# \di 查看索引
[基本数据库操作]========================
1. *创建数据库: create database [数据库名];
2. *查看数据库列表: \d
3. *删除数据库: . drop database [数据库名];
创建表: create table ([字段名1] [类型1]
*查看表名列表: \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 [表名];--删空整个表 ========================== ==========================
(4).PostgreSQL用户认证
PostgreSQL数据目录中的pg_hba.conf的作用就是用户认证,可以在/usr/local/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(表明任何操作系统用户都能够以同名数据库用户进行连接)
(5).看了那么多,来一个完整的创建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
(6).设定用户特定的权限
还是要用例子来说明:
创建一个用户组:
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 GROUP sale DROP USER sale2;
(7).备份数据库
可以使用pg_dump和pg_dumpall来完成。比如备份sales数据库:
pg_dump sales>/home/tk/pgsql/backup/1.bak

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager

LSOF (ListOpenFiles) is a command line tool mainly used to monitor system resources similar to Linux/Unix operating systems. Through the LSOF command, users can get detailed information about the active files in the system and the processes that are accessing these files. LSOF can help users identify the processes currently occupying file resources, thereby better managing system resources and troubleshooting possible problems. LSOF is powerful and flexible, and can help system administrators quickly locate file-related problems, such as file leaks, unclosed file descriptors, etc. Via LSOF Command The LSOF command line tool allows system administrators and developers to: Determine which processes are currently using a specific file or port, in the event of a port conflict

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features

Commonly used Maven packaging commands: 1. mvn clean package: This command will perform cleaning, compilation, testing and packaging operations; 2. mvn clean install: This command will perform cleaning, compilation, testing and installation operations; 3. mvn clean deploy: This command performs cleaning, compilation, testing, deployment, etc.
