在windows上使用PostgreSQL的连接服务文件(Connection Service File)连接到数据库中。
参考:https://www.postgresql.org/docs/11/libpq-pgservice.html
作者注:
PostgreSQL的连接服务文件(Connection Service File)存放了ip地址,数据库名,端口号等信息,作用类似于Oracle database的tnsnames.ora文件。
C:\>set lc_messages=C
C:\>d:\postgresql\pg11.5\bin\pg_config --sysconfdir
d:/postgresql/pg11.5/etc
C:\>d:\postgresql\pg11.5\bin\pg_config
BINDIR = d:/postgresql/pg11.5/bin
DOCDIR = d:/postgresql/pg11.5/doc
HTMLDIR = d:/postgresql/pg11.5/doc
INCLUDEDIR = d:/postgresql/pg11.5/include
PKGINCLUDEDIR = d:/postgresql/pg11.5/include
INCLUDEDIR-SERVER = d:/postgresql/pg11.5/include/server
LIBDIR = d:/postgresql/pg11.5/lib
PKGLIBDIR = d:/postgresql/pg11.5/lib
LOCALEDIR = d:/postgresql/pg11.5/share/locale
MANDIR = d:/postgresql/pg11.5/man
SHAREDIR = d:/postgresql/pg11.5/share
SYSCONFDIR = d:/postgresql/pg11.5/etc
PGXS = d:/postgresql/pg11.5/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = --enable-thread-safety --enable-nls --with-ldap --with-openssl --with-ossp-uuid --with-libxml --with-libxslt --with-icu --with-tcl --with-perl --with-python
CC = not recorded
CPPFLAGS = not recorded
CFLAGS = not recorded
CFLAGS_SL = not recorded
LDFLAGS = not recorded
LDFLAGS_EX = not recorded
LDFLAGS_SL = not recorded
LIBS = not recorded
VERSION = PostgreSQL 11.5
注意上面的输出:SYSCONFDIR = d:/postgresql/pg11.5/etc
在SYSCONFDIR = d:/postgresql/pg11.5/etc下建立名为pg_service.conf的文件(该文件的模板存放在SHAREDIR = d:/postgresql/pg11.5/share 目录中,可copy过来)
pg_service.conf的文件内容如下:
#
# Connection configuration file
#
# A service is a set of named connection parameters. You may specify
# multiple services in this file. Each starts with a service name in
# brackets. Subsequent lines have connection configuration parameters of
# the pattern "param=value" or LDAP URLs starting with "ldap://"
# to look up such parameters. A sample configuration for postgres is
# included in this file. Lines beginning with '#' are comments.
#
# Copy this to your sysconf directory (typically /usr/local/pgsql/etc) and
# rename it pg_service.conf.
#
#
#[postgres service name]
[pg12bt2-cluster] --->>>这个是服务名,请自行定义,在psql连接时使用。
host=192.168.80.187
dbname=postgres
user=pg12bt2
port=7432
下面做登录测试:
C:\>d:\postgresql\pg11.5\bin\psql service=pg12bt2-cluster
用户 pg12bt2 的口令:
psql (11.5, 服务器 12beta2)
警告:psql 主版本11,服务器主版本为12.
一些psql功能可能无法正常使用.
输入 "help" 来获取帮助信息.
postgres=# \q
注意:
1.PostgreSQL的连接服务文件(Connection Service File)中的关键字清单见:
https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-PARAMKEYWORDS
2.psql登录方式不止“psql service=pg12bt2-cluster”,
其他的登录方式请见:
https://www.postgresql.org/docs/11/libpq-pgservice.html
https://www.cybertec-postgresql.com/en/pg_service-conf-the-forgotten-config-file/

本文介绍在Windows环境中如何使用PostgreSQL的连接服务文件(pg_service.conf)来配置数据库连接参数,包括设置服务名、IP地址、数据库名、用户名和端口号。通过示例展示如何创建和使用此文件进行数据库连接。
2115

被折叠的 条评论
为什么被折叠?



