欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PostgreSQL psql安装、使用

程序员文章站 2022-03-15 16:26:02
...

1.下载

https://www.postgresql.org/download/

下载rpm安装包

PostgreSQL psql安装、使用

2. 安装

安装lib文件

rpm -ivh /home/wjm/pg/postgresql10-libs-10.11-1PGDG.rhel6.x86_64.rpm

[[email protected] packetbeat]# rpm -ivh /home/wjm/pg/postgresql10-libs-10.11-1PGDG.rhel6.x86_64.rpm 
warning: /home/wjm/pg/postgresql10-libs-10.11-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                ########################################### [100%]
   1:postgresql10-libs      ########################################### [100%]

 安装client

rpm -ivh /home/wjm/pg/postgresql10-10.11-1PGDG.rhel6.x86_64.rpm

[[email protected] packetbeat]# rpm -ivh /home/wjm/pg/postgresql10-10.11-1PGDG.rhel6.x86_64.rpm 
warning: /home/wjm/pg/postgresql10-10.11-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                ########################################### [100%]
   1:postgresql10           ########################################### [100%]

3. 基本命令

3.1 访问远程数据库

psql -h <hostname or ip> -p <端口> [数据库名称] [用户名称]

[[email protected] ~]$ psql -h 10.47.155.29 -p 5432 dbas_pg_test sndbasusr
Password for user sndbasusr: 
psql (10.11, server 10.7)
Type "help" for help.

dbas_pg_test=> 

 3.2 查看帮助

/?

dbas_pg_test=> \?
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview [COLUMNS] execute query and display results in crosstab
  \errverbose            show most recent error message at maximum verbosity
  \g [FILE] or ;         execute query (and send results to file or |pipe)
  \gexec                 execute query, then execute each value in its result
  \gset [PREFIX]         execute query and store results in psql variables
  \gx [FILE]             as \g, but forces expanded output mode
  \q                     quit psql
  \watch [SEC]           execute query every SEC seconds

Help
  \? [commands]          show help on backslash commands
  \? options             show help on psql command-line options
  \? variables           show help on special variables
  \h [NAME]              help on syntax of SQL commands, * for all commands

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
  \ev [VIEWNAME [LINE]]  edit view 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

3.2 显示所有数据库

/l

dbas_pg_test=> \l
                                 List of databases
     Name     |  Owner   | Encoding | Collate |   Ctype    |   Access privileges   
--------------+----------+----------+---------+------------+-----------------------
 dbas_pg_test | admin    | UTF8     | C       | zh_CN.UTF8 | 
 postgres     | postgres | UTF8     | C       | zh_CN.UTF8 | 
 template0    | postgres | UTF8     | C       | zh_CN.UTF8 | =c/postgres          +
              |          |          |         |            | postgres=CTc/postgres
 template1    | postgres | UTF8     | C       | zh_CN.UTF8 | =c/postgres          +
              |          |          |         |            | postgres=CTc/postgres
(4 rows)

3.3 选择数据库

\c [数据库名]

dbas_pg_test=> \c dbas_pg_test
psql (10.11, server 10.7)
You are now connected to database "dbas_pg_test" as user "sndbasusr".

3.4 表操作

3.4.1 查看数据库表

\d

dbas_pg_test=> \d
                   List of relations
 Schema |             Name              | Type  | Owner 
--------+-------------------------------+-------+-------
 public | pathman_cache_stats           | view  | admin
 public | pathman_concurrent_part_tasks | view  | admin

\d [表名] 

\d people

dbas_pg_test=> \d people
                      Table "public.people"
 Column |          Type          | Collation | Nullable | Default 
--------+------------------------+-----------+----------+---------
 name   | character varying(255) |           | not null | 
 sex    | character(255)         |           |          | 

3.5 退出

\q 

相关标签: 数据库