PostgreSQL学习第六篇--psql常用连接数据库的方法

时间:2023-01-30 06:21:15
psql -h <hostname or ip> -p <port> [dbname] [username]
-h 指定要连接数据库所在的主机名或者IP地址
-p 指定连接的数据库端口,最后两个参数是数据库名和用户名。
示例:
[postgres@single ~]$ psql -h 186.168.100.13 -p 5432 testdb postgres
psql: could not connect to server: Connection refused
        Is the server running on host "186.168.100.13" and accepting
        TCP/IP connections on port 5432?
[postgres@single ~]$ psql -h localhost -p 5432 testdb postgres
psql (9.6.1)
Type "help" for help.

[postgres@single pgdata]$ vi postgresql.conf

listen_addresses = '*'

[postgres@single pgdata]$ psql stop
psql: FATAL:  database "stop" does not exist
[postgres@single pgdata]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@single pgdata]$ pg_ctl start
server starting
[postgres@single pgdata]$ LOG:  database system was shut down at 2016-11-17 22:08:59 CST
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

 [postgres@single pgdata]$ psql -h 186.168.100.13 -p 5432 testdb postgres
FATAL:  no pg_hba.conf entry for host "186.168.100.13", user "postgres", database "testdb"
psql: FATAL:  no pg_hba.conf entry for host "186.168.100.13", user "postgres", database "testdb"

[postgres@single pgdata]$ vi pg_hba.conf
# IPv4 local connections:
host    all             all              186.168.100.13/32       trust 

[postgres@single pgdata]$ pg_ctl stop
LOG:  received fast shutdown request
LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
waiting for server to shut down....LOG:  database system is shut down
 done
server stopped
[postgres@single pgdata]$
[postgres@single pgdata]$ pg_ctl start
server starting
[postgres@single pgdata]$ LOG:  database system was shut down at 2016-11-17 22:16:14 CST
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

[postgres@single pgdata]$
[postgres@single pgdata]$ psql -h 186.168.100.13 -p 5432 testdb postgres
psql (9.6.1)
Type "help" for help.

testdb=#