PostgreSQL数据库软件安装后我们需要初始化数据库实例并注册服务后才能正常使用,关于PostgreSQL源码编码安装的方式大家参考上一步文章。
初始化实例
初始化通过命令行方式进行,使用initdb脚本,初始化命令帮助可以通过initdb –help查看,实例路径等无需提前创建,在创建实例时会自动创建,参考如下:
[postgres@localhost pgdata]$ /soft/postgresql/pgsql/bin/initdb -D /soft/postgresql/pgdata/data/ -U postgres -W -E utf8 --waldir=/soft/postgresql/pgdata/pgwal
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 "en_US.UTF-8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
Enter new superuser password:
Enter it again:
creating directory /soft/postgresql/pgdata/data ... ok
creating directory /soft/postgresql/pgdata/pgwal ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/Los_Angeles
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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:
/soft/postgresql/pgsql/bin/pg_ctl -D /soft/postgresql/pgdata/data/ -l logfile start
[postgres@localhost pgdata]$ ls
data pgwal
[postgres@localhost pgdata]$
配置环境变量
[postgres@localhost pgdata]$ cat >>~/.bash_profile <<'EOT'
> #PG15 configs
> export PG_HOME=/soft/postgresql/pgsql
> export PG_BIN=/soft/postgresql/pgsql/bin
> export PATH=$PATH:$PG_BIN
> export PGDATA=/soft/postgresql/pgdata/data
> cd $PGDATA
> EOT
[postgres@localhost pgdata]$ source ~/.bash_profile
注册数据库服务设置实现开机自启
官方yum源安装,会自动配置服务脚本,但是我们是通过源码编译安装,需要手动配置。
拷贝源码包的contrib目录中的服务脚本到/etc/init.d/目录中,将脚本重命名为postgresql,并授予可执行权限,此操作需要root用户操作。
[root@localhost ~]# cp /soft/postgresql/setup/postgresql-15.4/contrib/start-scripts/linux /etc/init.d/postgresql
[root@localhost ~]# chmod +x /etc/init.d/postgresql
[root@localhost ~]# vi /etc/init.d/postgresql
[root@localhost ~]# vi /etc/init.d/postgresql
[root@localhost ~]# vi /etc/init.d/postgresql
[root@localhost ~]# cat /etc/init.d/postgresql | grep soft
prefix=/soft/postgresql/pgsql
PGDATA="/soft/postgresql/pgdata/data"
[root@localhost ~]# cat /etc/init.d/postgresql | grep -i -E "prefix|pgdata"
# Installation prefix
#prefix=/usr/local/pgsql
prefix=/soft/postgresql/pgsql
#PGDATA="/usr/local/pgsql/data"
PGDATA="/soft/postgresql/pgdata/data"
注册数据库服务,redhat中需要用systemctl方式
[root@localhost ~]# systemctl enable postgresql
postgresql.service is not a native service, redirecting to systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable postgresql
[root@localhost ~]# /usr/lib/systemd/systemd-sysv-install enable postgresql
[root@localhost ~]# shutdown -r now
[root@localhost ~]# systemctl status postgresql.service
● postgresql.service - SYSV: PostgreSQL RDBMS
Loaded: loaded (/etc/rc.d/init.d/postgresql; generated)
Active: active (exited) since Tue 2023-08-15 01:03:20 PDT; 1min 15s ago
Docs: man:systemd-sysv-generator(8)
Process: 965 ExecStart=/etc/rc.d/init.d/postgresql start (code=exited, status=0/SUCCESS)
Aug 15 01:03:17 localhost.localdomain systemd[1]: Starting SYSV: PostgreSQL RDBMS...
Aug 15 01:03:17 localhost.localdomain su[985]: (to postgres) root on none
Aug 15 01:03:20 localhost.localdomain su[985]: pam_unix(su-l:session): session opened for user pos>
Aug 15 01:03:20 localhost.localdomain su[985]: pam_unix(su-l:session): session closed for user pos>
Aug 15 01:03:20 localhost.localdomain postgresql[965]: Starting PostgreSQL: ok
Aug 15 01:03:20 localhost.localdomain systemd[1]: Started SYSV: PostgreSQL RDBMS.
[root@localhost ~]#
数据库命令行登录
[root@localhost ~]# su - postgres
Last login: Tue Aug 15 01:03:20 PDT 2023
[postgres@localhost data]$ cd $PG_BIN
[postgres@localhost bin]$ psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
psql (15.4)
Type "help" for help.
postgres=#
原创文章,作者:lzb,如若转载,请注明出处:https://www.wlkjzx.com/2023/08/15/postgresql%e5%88%9d%e5%a7%8b%e5%8c%96%e6%95%b0%e6%8d%ae%e5%ba%93%e5%ae%9e%e4%be%8b%e5%b9%b6%e6%b3%a8%e5%86%8c%e6%9c%8d%e5%8a%a1%e5%bc%80%e6%9c%ba%e8%87%aa%e5%90%af-redhat8/
微信扫一扫