前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PostgreSQL数据库连接和数据库管理

PostgreSQL数据库连接和数据库管理

作者头像
SQLplusDB
发布2023-08-17 09:10:36
3240
发布2023-08-17 09:10:36
举报

PostgreSQL数据库连接和数据库管理

首先我们需要通过multipass启动安装了PostgreSQL的虚拟机,然后我们就可以开启PostgreSQL数据库之旅了。

代码语言:javascript
复制
multipass list
multipass shell <虚拟机名>

例:

代码语言:javascript
复制
C:\Users\Administrator>multipass list
Name                    State             IPv4             Image
mysql-vm                Stopped           --               Ubuntu 20.04 LTS
pgvm                    Running           N/A              Ubuntu 20.04 LTS

C:\Users\Administrator>multipass shell pgvm
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-125-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Sep 15 21:25:06 CST 2022

  System load:  0.48              Processes:               106
  Usage of /:   29.2% of 4.67GB   Users logged in:         0
  Memory usage: 20%               IPv4 address for enp0s3: 10.0.2.15
  Swap usage:   0%


0 updates can be applied immediately.


The list of available updates is more than a week old.
To check for new updates run: sudo apt update
New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@pgvm:~$

PostgreSQL数据库连接

连接PostgreSQL命令

可以通过如下命令利用自带的psql工具连接和断开PostgreSQL服务器。

代码语言:javascript
复制
--连接数据库
shell> psql -h <主机名>  -p <端口号> -U <用户名> <数据库名>

例:

代码语言:javascript
复制
ubuntu@pg-vm:~$ psql -U postgres -h localhost -d postgres
Password for user postgres:                --》输入之前设置的密码:pass
psql (12.12 (Ubuntu 12.12-0ubuntu0.20.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=#

断开PostgreSQL命令

对于PostgreSQL,通常使用\q推出连接。 但是有时候和MySQL一样,也可以通过输入exit或者quit或者Control+D也能够退出PostgreSQL连接。

例:

代码语言:javascript
复制
--首选 \q
postgres=# \q
或者
postgres=# exit
或者
postgres=# quit
或者
Control+D

PostgreSQL数据库基本操作

执行查询

和MySQL数据库相类似,连接了数据库以后就可以直接执行查询。

例:

代码语言:javascript
复制
postgres=# select version();

                                                               version                                                 
----------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.12 (Ubuntu 12.12-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)

(END)
psql命令行工具

psql是PostgreSQL自带的命令行工具,功能全面,是PostgreSQL数据库最重要的命令行工具之一。 通过psql工具可以和PostgreSQL数据库服务器进行SQL命令行交互。另外,psql工具也提供了大量强大的元命令(以反斜杠“\”开头的命令)。

通过在psql命令行键入help可以获得使用帮助的内容。

例:

代码语言:javascript
复制
postgres=# help
You are using psql, the command-line interface to PostgreSQL.
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

psql工具元命令可以帮助我们更有效的利用数据库,下面是一些常用的元命令。

例1:列出所有的数据库

代码语言:javascript
复制
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(3 rows)

例2:列出所有的 schema

代码语言:javascript
复制
postgres=# \dn
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 row)

例3:列出所有的数据库用户和角色

代码语言:javascript
复制
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \dg
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

例4:获得连接信息

代码语言:javascript
复制
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "localhost" (address "::1") at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)

注:通过在psql命令行键入 ? 可以获得更多元命令的帮助。

数据库操作

PostgreSQL数据库操作相关的常用命令如下:

代码语言:javascript
复制
查看数据库列表:/l
create database <数据库名>;
drop database <数据库名>;
切换数据库:\c <数据库名>
CREATE DATABASE创建数据库

可以通过CREATE DATABASE创建一个新的PostgreSQL数据库。

例1:创建新数据库

代码语言:javascript
复制
postgres=# create database mydb1;
CREATE DATABASE

例2:创建指定owner的数据库

代码语言:javascript
复制
postgres=# create database mydb2 owner user1 ;
CREATE DATABASE

查看已经存在的数据库

使用 \l 命令可以 查看已经存在的数据库。

例:

代码语言:javascript
复制
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 mydb1     | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 mydb2     | user1    | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(5 rows)

ALTER DATABASE修改数据库

通过ALTER DATABASE命令更改一个数据库的属性。

例:修改数据库的所有者

代码语言:javascript
复制
postgres-# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 mydb1     | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 mydb2     | user1    | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(5 rows)

postgres=#
postgres=#
postgres=# alter database mydb1 owner to user1;
ALTER DATABASE
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 mydb1     | user1    | UTF8     | C.UTF-8 | C.UTF-8 |
 mydb2     | user1    | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(5 rows)

\c 切换数据库

可以通过 \c <数据库名>命令切换数据库。

代码语言:javascript
复制
postgres=# \c mydb1
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
You are now connected to database "mydb1" as user "postgres".
mydb1=# \conninfo
You are connected to database "mydb1" as user "postgres" on host "localhost" (address "::1") at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
mydb1=#
查看数据库的版本
代码语言:javascript
复制
postgres=# SELECT version();

----------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.11 (Ubuntu 12.11-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)

(END)

或者

代码语言:javascript
复制
postgres@pg-vm:~$ psql --version
psql (PostgreSQL) 12.11 (Ubuntu 12.11-0ubuntu0.20.04.1)

参考

https://www.postgresql.org/docs/14/managing-databases.html

Chapter 23. Managing Databases

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 SQL和数据库技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • PostgreSQL数据库连接和数据库管理
    • PostgreSQL数据库连接
      • 连接PostgreSQL命令
      • 断开PostgreSQL命令
    • PostgreSQL数据库基本操作
      • 执行查询
      • 数据库操作
    • 参考
    相关产品与服务
    数据库管理
    数据库管理(Database Management Center,DMC)是一个高效,安全,可靠的数据库一站式管理平台。DMC 提供可视化的库管理、实例会话管理、SQL 窗口、SQL 安全审计、SQL 变更审批、实时监控、操作审计等数据库管理能力,集成诊断优化和数据可视化分析能力,从而简化和规范数据库管理操作、降低数据库运维门槛、提升运维效率。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档