PostgreSQL学习(1)

安装PostgreSQL

sudo apt install postgresql postgresql-contrib
# 如果您需要特定版本,请使用“postgresql-12”或类似版本,替换“postgresql”:

postgresql-contrib软件包包含了一些有用的扩展和附加功能。

登录PostgreSQL

默认情况下,PostgreSQL安装后只允许本地连接,且只有postgres用户。以postgres用户登录,使用以下命令:

sudo su - postgres
psql
#或者
sudo -u postgres psql

这将进入PostgreSQL命令行界面。

设置密码

postgres=# \password postgres
#或者
postgres=# alter user postgres password with 'new_password';

创建删除数据库mydb

#创建
postgres=# CREATE DATABASE mydb;
#删除
postgres=# DROP DATABASE mydb;

创建PostgreSQL角色myuser

postgres=# CREATE USER myuser WITH PASSWORD 'mypassword';

授予权限

postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

在命令行界面登录到PostgreSQL:

psql -d mydb -U myuser -h localhost -p 5432

远程登录

1、修改文件: /etc/postgresql/13/main/postgresql.conf , 修改listen_addresses的值

listen_addresses = '*'

2、在 pg_hba.conf 文件最后添加如下内容

echo "host all all 0/0 md5" >> /etc/postgresql/13/main/pg_hba.conf
posted @ 2023-10-10 10:19  华小电  阅读(18)  评论(0编辑  收藏  举报