PostgreSQL 基本操作

本文介绍了如何在Ubuntu20上安装PostgreSQL,提供了一系列实用的管理工具操作命令,包括密码重置过程。主要内容包括连接方式、数据库列表、角色管理以及密码更改等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 PostgreSQL: The world's most advanced open source database

上面是官方网站。下载和安装直接去这里。

在命令行输入psql -U postgres

然后输入密码,安装时设置的密码器

就进入交互式postgreSQL操作。

在Ubuntu20里, 没有提示密码,如下操作进入:

postgres=# \q
sammy@liwen:~$ sudo -u postgres psql
[sudo] password for sammy:
psql (16.2 (Ubuntu 16.2-1.pgdg20.04+1))
Type "help" for help.

postgres=# \l
                                                   List of databases
   Name    |  Owner   | Encoding | Locale Provider | Collate |  Ctype  | ICU Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+---------+---------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 |            |           |
 template0 | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |         |         |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |         |         |            |           | postgres=CTc/postgres
(3 rows)

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

postgres=#

好的教程PostgreSQL Tutorial

管理工具操作 PostgreSQL Administration

主要操作命令  参考:

17 Practical psql Commands That You Don't Want To Miss

退出 \q 

列出可用的数据库  \l

更改当前数据库  \c dbname username

没用户名就是当前用户

列出表 \dt

看表的信息  \d table_name

列出可用schema \dn

列出可用函数  \df

列出可用视图 \dv

列出用户和角色  \du

更改用户密码

ALTER USER user_name WITH PASSWORD 'new_password';

postgres=# alter user postgres with password '12345678';
ALTER ROLE
postgres=#

忘记密码恢复操作

找到文件: pg_hba.conf

我的是在:C:\Program Files\PostgreSQL\16\data

我的 ubuntu 20 是 /etc/postgresql/12/main

Backup pg_hba.conf

编辑该文件:from md5 (or scram-sha-256 in a newer version) to trust

重启数据库:在windows的控制面板中,找到管理工具,然后找到服务:postgresql,点击右键 ,重启就行了。

在命令行 psql -U postgres

这个可以不用密码登录。

按上面方法更改密码

重新编辑 pg_hba.conf,改回原来的状态。

重启数据库

可以用设置的新密码登录了。

参考如下:

How To Reset Forgotten Password Of postgres User

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值