一、安裝前准備工作
新建用戶
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