前言:PostgreSQL里连接数据库我们比较常见使用psql进行连接,因为PostgreSQL一个实例里有多个数据库,如果我们想连接到其它数据库,除了可以退出再重连也可以直接使用元命令connect进行数据库连接切换,下面详细介绍一下不同参数使用方式:
1. 常见psql连接数据库
$ psql -h127.0.0.1 -Upostgres postgres psql (12.1) Type "help" for help. postgres=# \conninfo You are connected to database "postgres" as user "postgres" on host "127.0.0.1" at port "5432". postgres=#
复制
使用元命令\conninfo可以查看数据库连接信息,包括主机、端口、数据库名称、数据库用户
2.同一用户切换到其他数据库
test=# \connect test You are now connected to database "test" as user "postgres".
复制
当前登录用户为postgres,登录用户不变,切换到test数据库
3. 同一库切换登录用户
test=# \connect - test Password for user test: You are now connected to database "test" as user "test".
复制
当前数据库为test,数据库不变,登录用户由postgres切换到test
单独切换用户,需要加 “-”
4.当前实例同时切换数据库和登录用户
test=> \conninfo You are connected to database "test" as user "test" via socket in "/var/run/postgresql" at port "5432". test=> \connect postgres postgres You are now connected to database "postgres" as user "postgres". postgres=>
复制
当前数据库为test,登录用户为test,切换数据库为postgres,且切换登录用户为postgres
5.切换主机和数据库
$ psql -Upostgres psql (12.1) Type "help" for help. postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432". postgres=# \connect test - 192.168.99.200 You are now connected to database "test" as user "postgres" on host "192.168.99.200" at port "5432".
复制
主机参数前面需要加 “-”
6.切换主机、数据库和登录用户(端口为默认)
$ psql -Upostgres psql (12.1) Type "help" for help. postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432". postgres=# \connect test test 192.168.99.200 Password for user test: You are now connected to database "test" as user "test" on host "192.168.99.200" at port "5432".
复制
7.切换主机、数据库和登录用户以及端口
$ psql -Upostgres psql (12.1) Type "help" for help. postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432". postgres=# \connect postgres test 192.168.99.200 5432 Password for user test: You are now connected to database "postgres" as user "test" on host "192.168.99.200" at port "5432".
复制
8. 使用conninfo
$ psql psql (12.1) Type "help" for help. postgres=> \c "hostaddr=192.168.137.11 port=5432 user=repuser password=xxxxxx dbname=postgres" Password for user repuser: You are now connected to database "postgres" as user "repuser".
复制
最新版本13.2,12.6,11.11,10.16,9.6.21,9.5.25支持connection_string参数中使用密码。
下面是版本12.6可以使用password
$ psql psql (12.6) Type "help" for help. postgres=# \c "hostaddr=192.168.137.11 port=5432 user=repuser password=xxxxxx dbname=postgres" You are now connected to database "postgres" as user "repuser" on host "192.168.137.11" at port "5432".
复制
往期回顾
保持联系
从2019年12月开始写第一篇文章,分享的初心一直在坚持,本人现在组建了一个PG乐知乐享交流群,欢迎关注我文章的小伙伴进群吹牛唠嗑,交流技术,互赞文章。
如果群二维码失效可以加我微信。
最后修改时间:2022-10-23 10:34:13
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
相关阅读
外国CTO也感兴趣的开源数据库项目——openHalo
小满未满、
1646次阅读
2025-04-21 16:58:09
9.9 分高危漏洞,尽快升级到 pgAdmin 4 v9.2 进行修复
严少安
386次阅读
2025-04-11 10:43:23
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
372次阅读
2025-04-15 14:48:05
转发有奖 | PostgreSQL 16 PGCM高级认证课程直播班招生中!
墨天轮小教习
183次阅读
2025-04-14 15:58:34
中国PostgreSQL培训认证体系新增PGAI应用工程师方向
开源软件联盟PostgreSQL分会
182次阅读
2025-05-06 10:21:13
PG生态赢得资本市场青睐:Databricks收购Neon,Supabase融资两亿美元,微软财报点名PG
老冯云数
141次阅读
2025-05-07 10:06:22
SQL 优化之 OR 子句改写
xiongcc
136次阅读
2025-04-21 00:08:06
告别老旧mysql_fdw,升级正当时
NickYoung
121次阅读
2025-04-29 11:15:18
PostgreSQL中文社区亮相于第八届数字中国峰会
PostgreSQL中文社区
105次阅读
2025-05-07 10:06:20
PostgreSQL的dblink扩展模块使用方法
szrsu
103次阅读
2025-04-24 17:39:30