[root@weekend02 postgres]# source .bash_profile
八、切换用户到postgres并使用initdb初使用化数据库
[root@weekend02 postgres]# su - postgres
[postgres@weekend02 ~]$ initdb
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.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
fixing permissions on existing directory /pgsql/postgresql/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:
[postgres@weekend02 data]$ ls
base pg_ident.conf pg_serial pg_tblspc postgresql.auto.conf
global pg_logical pg_snapshots pg_twophase postgresql.conf
pg_commit_ts pg_multixact pg_stat PG_VERSION
pg_dynshmem pg_notify pg_stat_tmp pg_wal
pg_hba.conf pg_replslot pg_subtrans pg_xact
九、配置服务
修改/pgsql/postgresql/data目录下的两个文件。
postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
pg_hba.conf 配置对数据库的访问权限。
[postgres@weekend02 data]$ vi postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。
[postgres@weekend02 data]$ vi pg_hba.conf
找到最下面这一行 ,这样局域网的人才能访问。红色为新添加内容。
# IPv4 local connections:
host all all 0.0.0.0/0 trust
host all all 127.0.0.1/32 trust
[postgres@weekend02 pgsql]$ cd /pgsql/postgresql-11.1/contrib/start-scripts
[postgres@weekend02 start-scripts]$ ls
freebsd linux macos
1)切换为root用户,修改linux文件属性,添加X属性
[root@weekend02 start-scripts]# chmod a+x linux
2) 复制linux文件到/etc/init.d目录下,更名为postgresql
[root@weekend02 start-scripts]# cp linux /etc/init.d/postgresql
3)修改/etc/init.d/postgresql文件的两个变量
postgres=# create user pg password 'pg';
CREATE ROLE
postgres=# ALTER ROLE pg SUPERUSER;
ALTER ROLE
postgres=# create database pg;
CREATE DATABASE
postgres=# \q
重新登录数据库
输入密码
显示数据库
[postgres@weekend02 ~]$ psql -U pg -d pg -h weekend02
psql (11.1)
Type "help" for help.