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 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,改回原来的状态。
重启数据库
可以用设置的新密码登录了。
参考如下: