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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)