PostgreSQL的安装
1.1 使用ctrl+alt+t打开终端并安装
sudo apt-get install postgresql-9.5
上面是安装9.5版本,如果是安装最新版本,则不需要写版本号。
安装成功后,会给操作系统自动添加一个名为postgres的操作系统用户,密码随机。
并自动生成一个名为postgres的数据库,用户名为postgres,密码随机。
登录postgre SQL数据库:
psql -U postgres
如果数据库配置文件(/etc/postgresql/14/main/pg_hba.conf
)里配置的是peer模式,则是使用unix domain socket机制登录,即需要当前的操作系统登录用户也是postgres这个用户。
数据库配置文件 /etc/postgresql/14/main/pg_hba.conf
:
# Database administrative login by Unix domain socket
local all postgres trust
如果数据库配置文件里配置的是trust的模式,则可以直接登录到PostgreSQL,进入PostgreSQL的命令行:
ubuntu@ip-xxxxx:~$ psql -U postgres
psql (14.5 (Ubuntu 14.5))
Type "help" for help.
postgres=#
如果数据库配置文件里配置的是使用密码的模式,则会提示输入密码:
ubuntu@ip-xxxxx:~$ psql -U postgres
Password for user postgres:
参考:
postgresql 安装使用修改密码psql登录
PostgreSQL数据库设置登录数据库密码
重启数据库
sudo systemctl restart postgresql
数据库操作
先登录postgre SQL数据库:psql -U postgres
数据库
查看所有数据库
postgres=# \l
查看单个数据库的大小
postgres=# select pg_database_size('postgres');
查看所有数据库的大小
postgres=# select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database;
以KB,MB,GB的方式来查看数据库大小
postgres=# select pg_size_pretty(pg_database_size('postgres'));
数据表
查看当前数据库下所有表
postgres=# \d
查看test表定义
postgres=# \d test
PostgreSQL 登录报错 psql: FATAL: Peer authentication failed for user
psql的连接建立于Unix Socket上默认使用peer authentication,所以必须要用和 数据库用户 相同的 系统用户 进行登录。
还有一种方法,将peer authentiction 改为 md5,并给数据库设置密码。修改配置文件,
/etc/postgresql/x.x/main/pg_hba.conf
local all postgres peer
// change to
local all postgres md5
参考:
PostgreSQL 登录报错 psql: FATAL: Peer authentication failed for user
查看PostgreSQL的版本
psql --version
JDBC的URL的格式
jdbc的格式:
jdbc:mysql://[host][,failoverhost...][:port]/[database] [?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
例如:
jdbc:mysql://127.0.0.1/uba_db?useUnicode=true&characterEncoding=UTF-8
URL用于标识数据库的位置,通过URL地址告诉JDBC程序连接哪个数据库,postgre jdbc支持的URL的写法为:
jdbc:postgresql:database
jdbc:postgresql:/
jdbc:postgresql://host/database
jdbc:postgresql://host/
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/
jdbc:postgresql://host1:port1,host2:port2/database
ipv6地址:
jdbc:postgresql://[::1]:5740/database
也可以在url中加入其他参数如:
?uer=root&password=root&useUnicode=true&characterEncoding=utf8