pg默认数据库连接

pg数据库连接

系统用户默认数据库连接

以系统用户认证连接默认数据库时,若系统用户没有匹配到cluster中任意数据库时,数据库连接一定会失败的,针对于该情况,可通过如下方法加以解决

  • 明确指定要连接的数据库
  • 修改数据库名

场景扩展

通常情况下,pg的系统用户是postgres,与内置数据库一致,但出于安全或管理等因素,不用常规系统用户postgres

  • 在一数据库服务器上,部署多个cluster,每个cluster由不同dba管理
  • 基于安全因素,可避开常规系统用户(postgres)

验证过程

增加系统用户pgxusr

[root@cfg3 ~]# useradd pgxusr
[root@cfg3 ~]# passwd pgxusr
Changing password for user pgxusr.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

创建数据库cluster目录

[root@cfg3 ~]# mkdir -p /var/dbdata/pgxusr
[root@cfg3 ~]# chown -R pgxusr /var/dbdata/pgxusr/
[root@cfg3 ~]#
[root@cfg3 ~]# su - pgxusr

初始化数据库cluster

[pgxusr@cfg3 ~]$ pg_ctl init -D /var/dbdata/pgxusr/
The files belonging to this database system will be owned by user “pgxusr”.
This user must also own the server process.

The database cluster will be initialized with locale “en_US”.
The default database encoding has accordingly been set to “LATIN1”.
The default text search configuration will be set to “english”.

Data page checksums are disabled.

fixing permissions on existing directory /var/dbdata/pgxusr … ok
creating subdirectories … ok
selecting dynamic shared memory implementation … posix
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting default time zone … PRC
creating configuration files … ok
running bootstrap script … ok
performing post-bootstrap initialization … ok
syncing data to disk … ok

initdb: warning: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/pgsql/bin/pg_ctl -D /var/dbdata/pgxusr -l logfile start

修改port

[pgxusr@cfg3 ~]$ vim /var/dbdata/pgxusr/postgresql.conf
[pgxusr@cfg3 ~]$
[pgxusr@cfg3 ~]$

启动cluster

[pgxusr@cfg3 ~]$ pg_ctl start -D /var/dbdata/pgxusr/
waiting for server to start…2021-03-09 16:22:17.837 CST [60219] LOG: starting PostgreSQL 13.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
2021-03-09 16:22:17.870 CST [60219] LOG: listening on IPv6 address “::1”, port 5438
2021-03-09 16:22:17.870 CST [60219] LOG: listening on IPv4 address “127.0.0.1”, port 5438
2021-03-09 16:22:17.871 CST [60219] LOG: listening on Unix socket “/tmp/.s.PGSQL.5438”
2021-03-09 16:22:17.873 CST [60220] LOG: database system was shut down at 2021-03-09 16:21:06 CST
2021-03-09 16:22:17.876 CST [60219] LOG: database system is ready to accept connections
done
server started

查看cluster状态

[pgxusr@cfg3 ~]$ pg_ctl status -D /var/dbdata/pgxusr/
pg_ctl: server is running (PID: 60219)
/usr/pgsql/bin/postgres “-D” “/var/dbdata/pgxusr”
[pgxusr@cfg3 ~]$

[pgxusr@cfg3 ~]$ psql -p5438 -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------±-------±---------±--------±------±------------------
postgres | pgxusr | LATIN1 | en_US | en_US |
template0 | pgxusr | LATIN1 | en_US | en_US | =c/pgxusr +
| | | | | pgxusr=CTc/pgxusr
template1 | pgxusr | LATIN1 | en_US | en_US | =c/pgxusr +
| | | | | pgxusr=CTc/pgxusr
(3 rows)

连接数据库

[pgxusr@cfg3 ~]$ psql -p5438
2021-03-09 16:24:02.295 CST [60258] FATAL: database “pgxusr” does not exist
psql: error: FATAL: database “pgxusr” does not exist
[pgxusr@cfg3 ~]$

明确指定数据库连接

[pgxusr@cfg3 ~]$ psql -p5438 postgres
psql (13.2)
Type “help” for help.

postgres=# alter database postgres rename to pgxusr ;
2021-03-09 16:25:27.555 CST [60267] ERROR: current database cannot be renamed
2021-03-09 16:25:27.555 CST [60267] STATEMENT: alter database postgres rename to pgxusr ;
ERROR: current database cannot be renamed
postgres=#
postgres=# \c template1
You are now connected to database “template1” as user “pgxusr”.
template1=#
template1=# \c
You are now connected to database “template1” as user “pgxusr”.
template1=#
template1=#
template1=# alter database postgres rename to pgxusr ;
ALTER DATABASE
template1=#
template1=# \c pgxusr
You are now connected to database “pgxusr” as user “pgxusr”.
pgxusr=#
pgxusr=# \q

默认连接

[pgxusr@cfg3 ~]$ psql -p5438
psql (13.2)
Type “help” for help.

pgxusr=# create database mydb ;
CREATE DATABASE
pgxusr=#
pgxusr=#

你可能感兴趣的:(postgresql默认连接,postgresql)