连接到一个数据库

最近更新时间: 2024-06-12 15:06:00

  • 使用参数连接。

     [tbase@VM_0_29_centos root]$ psql -h 172.16.0.29 -p 15432 -U tbase -d postgres
     psql (PostgreSQL 10 (TBase 2.01))
     Type "help" for help.
     postgres=# 
  • 使用conninfo字符串或者一个URI。

     [tbase@VM_0_29_centos root]$ psql postgresql://tbase@172.16.0.29:15432/postgres 
     psql (PostgreSQL 10 (TBase 2.01))
     Type "help" for help.
     postgres=#
  • 配置证书连接。

     [tbase@VM_0_29_centos ~]$ psql 'host=172.16.0.29 port=15432 dbname=postgres user=tbase sslmode=verify-ca sslcert=postgresql.crt sslkey=postgresql.key sslrootcert=root.crt'
     Password: 
     psql (PostgreSQL 10 (TBase 2.01))
     SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
     Type "help" for help.
     postgres=> \q
  • 配置环境变量后快捷连接。

     [tbase@VM_0_29_centos root]$ PGHOST=127.0.0.1
     [tbase@VM_0_29_centos root]$ PGDATABASE=postgres
     [tbase@VM_0_29_centos root]$ PGPORT=15432
     [tbase@VM_0_29_centos root]$ export PGHOST PGUSER PGDATABASE PGPORT
     [tbase@VM_0_29_centos root]$ psql
     psql (PostgreSQL 10 (TBase 2.01))
     Type "help" for help.
     postgres=# 

    也可以配置在用户环境变量中。

     [tbase@VM_0_29_centos root]$ cat ~/.bashrc
     .bashrc
     PGUSER=tbase
     PGHOST=127.0.0.1
     PGDATABASE=postgres
     PGPORT=15432
     export PGHOST PGUSER PGDATABASE PGPORT