postgresql初始化数据库

postgresql初始化数据库

目录/var/lib/pgsql/data

  1. 创建pgsql的数据库:
# mkdir -p /var/lib/pgsql/data 
  1. 改变目录的所属用户
# cd /var/lib/pgsql 
# chown postgres.postgres data
  1. 切换到postgres
# su  postgres 
bash-4.2$ initdb -E UTF-8 -D /var/lib/pgsql/data --locale=en_US.UTF-
The files belonging to this database system will be owned by user "p
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:  “tower”
Enter it again: “tower”

fixing permissions on existing directory /var/lib/pgsql/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, o
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /var/lib/pgsql/data -l logfile start

初始化完成

修改配置文件
1.修改/var/lib/pgsql/data/postgresql.conf

listen_addresses = '*'		#要监听的IP地址;
							# 以逗号分隔的地址列表;
							# 默认为'localhost'; '*'所有
port = 5432					# (更改需要重新启动)
  1. 修改/var/lib/pgsql/data/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0           trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

启动并查看

# su postgres   //启动  
bash-4.2$ /opt/rh/rh-postgresql10/root/usr/bin/pg_ctl -D /var/opt/rh/rh-postgresql10/lib/pgsql/data -l logfile start
waiting for server to start.... done
server started

[root@postgresql]# netstat -tpnl |grep 5432  
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      22852/postgres      
tcp6       0      0 ::1:5432                :::*                    LISTEN      22852/postgres 

用postgres用户登录,并改密码

[root@rudder data]# su postgres  //切换用户  
bash-3.2$ psql -U postgres    //连接pgsql server  
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.  
  
Type:  \copyright for distribution terms  
       \h for help with SQL commands  
       \? for help with psql commands  
       \g or terminate with semicolon to execute query  
       \q to quit  
  
postgres=# Alter USER postgres WITH PASSWORD '***密码**';  //添加密码  
ALTER ROLE        //出现这个才算成功,第一次操作没成功,pgadmin连不上  
postgres-# \q     //退出  

问题:

bash-4.2$ psql -U postgres
psql: FATAL:  role "postgres" does not exist

你可能感兴趣的:(数据库)