PostgreSQL 二进制安装

一、安装前准备工作

新建用户

sudo groupadd sql
sudo useradd -g sql postgres
sudo passwd postgres

创建数据及日志目录,并做相应授权

sudo mkdir -p /home/SQL/Data/pgsql/{data,log}
sudo chown -R postgres.sql /home/SQL/Data/pgsql/

注:可以添加一个软链接方便进入pgsql目录:ln -s /home/SQL/PostgreSQL/pgsql/ pgsql

二、进行数据库初始化

切换用户 postgres,并执行初始化操作

su postgres
cd /usr/local/pgsql/bin
./initdb -E utf8 -D /home/SQL/PostgreSQL/Data/data

初始化完成提示

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.utf8".
initdb: could not find suitable text search configuration for locale "zh_CN.utf8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /home/SQL/PostgreSQL/Data/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

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:

    ./pg_ctl -D /home/SQL/PostgreSQL/Data/data -l logfile start

三、安装后

配置环境变量,~/.bash_profile 添加如下内容

PATH=/usr/local/pgsql/bin:$PATH
export PATH

注:这个文件目录是在当前用户的根目录下,即/home/{User}/

四、启动 & 登陆

启动数据库

./pg_ctl -D /home/SQL/PostgreSQL/Data/data  -l /home/SQL/PostgreSQL/Data/log/postgres.log start
或
./postgres -D /home/SQL/PostgreSQL/Data/data > //home/SQL/PostgreSQL/Data/log/postgres.log &

登陆数据库

./psql

添加新用户和创建数据库

create user admin with password '××××××';
create database mydb with encoding='utf8' owner=admin;

验证登录

./psql -U admin -d mydb

退出并关闭数据库

创建表之后可以使用 \d 表名; 查看表的详细信息
使用 \l 查看当前的数据库列表
执行 \q 退出交互式界面
./pg_ctl -D /home/SQL/PostgreSQL/Data/data/ stop

连接远程数据库

-h参数指定服务器地址,默认为127.0.0.1
-d指定连接之后选中的数据库,默认也是postgres
-U指定用户,默认是当前用户
-p 指定端口号,默认是"5432"

其它更多的参数选项可以执行:./psql --help 查看

{pgsql安装目录}/bin/psql -h {服务器IP} -d postgres -U postgres -p 5432
如:
./psql -h 127.0.0.1 -d mydb -U postgres -p 5432

猜你喜欢

转载自www.cnblogs.com/WangBoBlog/p/9155093.html