安装包获取
https://www.postgresql.org/ftp/source/v12.3/ postgres 安装包的下载路径
update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'your_database';
操作步骤
(1)创建postgresql安装路径
mkdir /opt/postgresql
(2)解压缩安装包,安装postgresql
tar xzvf postgresql-12.3.tar.gz
cd postgresql-12.3
./configure --prefix=/opt/postgresql
make
make install
(3)创建postgresql用户,并修改密码
useradd postgresql
passwd postgresql
(4)创建数据库存放目录与设置权限
mkdir /opt/postgresql/data
chown -R postgresql:postgresql /opt/postgresql/data
(5)初始化实例
su - postgresql
cd /opt/postgresql/bin
./initdb -D /opt/postgresql/data
(6)开启远程访问
在安装目录下data/postgresql.confi文件中将
在data/pg_hba.conf中,增加如下标红的一行
TYPE DATABASE USER ADDRESS METHOD
IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
其中0.0.0.0/0表示运行任意ip地址访问。
若设置为 192.168.1.0/24 则表示允许来自ip为192.168.1.0 ~ 192.168.1.255之间的访问。使用(postgresql用户):
启动数据库
./pg_ctl -D /opt/postgresql/data start
查看postgres进程:
ps -ef | grep postgres
停止postgresql数据库
./pg_ctl -D /opt/postgresql/data stop -m fast
创建用户数据库
./createdb test
登录命令行
./psql test
创建用户SQL
CREATE USER test WITH PASSWORD 'test';
执行SQL文件
./psql -U dm -W -d dm -f ../../sql/dm.sql
文件加载
copy test2 from '/dbfw_dc/data/public_test2.csv' with (FORMAT csv,DELIMITER
',',escape '\',header true,quote '"',encoding 'UTF8')
文件加载2
cat /dbfw_dc/data/public_test3.csv | ./psql -U test -c "copy test3 from stdin with
(FORMAT csv,DELIMITER ',',escape '\',quote '\"',encoding 'UTF8')" -d test1
查看参数
select * from pg_settings where upper(name) ='CHECKPOINT_SEGMENTS'
修改参数
set CHECKPOINT_SEGMENTS to default
修改参数2
postgresql.conf文件
1.查看服务端的字符集
show server_encoding;
2.查看客户端的字符集
show client_encoding;
3.修改客户端的字符集:
set client_encoding to 'gbk';
查看pg服务启动的时间
select pg_postmaster_start_time();
https://www.postgresql.org/ftp/source/v11.5/
$psql -U user_name -d database_name -h serverhost
\h
\?
\d
\q
\d
\x
\dT+
\dx
\l
\! hostname
\timing
\c database_name
set search to schema
explain analyze sql
jdbc驱动
https://jdbc.postgresql.org/download.html
创建用户:
创建模式:语法结构
create schema if not exists schema_name [authorization username] [schema_element ....]
example:
create schema test_schema authorization with passwod 123456;
schema_name
模式名称,缺省使用user_name,且不能以pg_开头
user_name
模式属于的用户,缺省为执行命令的用户
schema_element
一条sql语句,即创建模式后,在该模式下创建一个数据库对象,当前支持的子句有:create table,create view,create index,create sequence,create trigger and grant
if not exists
如果模式已存在,使用该选项不会抛出错误。使用此选项不能使用schema_element子句
模式修改:
alter schema name rename to new_name;
alter schema name owner to new_owner;
参数 :
name
模式名称
new_name
模式新的名称,同样新名称也不能以pg_开头
new_owner
模式新用户名称
模式删除:
Syntax:
drop schema [if exists] name [, ...] [ cascade |restrict ]
参数:
if exists
如果模式不存在,不会抛出错误。
name
模式名称
cascade
自动删除该模式下数据库对象
restrict
如果该模式下还存在数据库对象,则不允许删除该模式,RESTRICT为缺省值