DEBIAN安装postgresql


在 Debian 系统上安装 PostgreSQL 可以按照以下步骤进行:

  1. 更新软件包列表:

    sudo apt update
  2. 安装 PostgreSQL:

    sudo apt install postgresql postgresql-contrib

    这个命令会安装 PostgreSQL 服务器以及一些额外的工具和扩展。

  3. 安装过程中,可能会提示你设置一个 PostgreSQL 用户的密码。这个用户通常是 postgres,它是 PostgreSQL 的默认超级用户。请设置一个安全的密码并记住它。
  4. 启动 PostgreSQL 服务:

    sudo systemctl start postgresql

    如果你希望 PostgreSQL 在系统启动时自动启动,可以使用以下命令:

    sudo systemctl enable postgresql
  5. 验证安装:

    • 可以使用以下命令检查 PostgreSQL 服务的状态:

      sudo systemctl status postgresql
    • 你也可以尝试使用 psql 命令连接到 PostgreSQL 数据库服务器:

      sudo -u postgres psql

      这将以 postgres 用户身份连接到本地的 PostgreSQL 服务器。如果连接成功,你将看到一个 postgres=# 的提示符,表示你已经进入了 PostgreSQL 的命令行界面。

现在,你已经在 Debian 系统上成功安装了 PostgreSQL。你可以使用 PostgreSQL 的命令行工具或图形化管理工具来管理数据库。

创建新用户:

CREATE ROLE jroot WITH LOGIN PASSWORD 'JrPGL737888';
ALTER ROLE jroot CREATEDB;
CREATE DATABASE my_database OWNER jroot;
psql -U jroot -d my_database

psql -U jroot -d my_database -W JrPGL737888

postgresql PGSpp887788

关键字:debian、postgresql、安装

时间:2023年11月16日

一、前言

Debian11安装PostgreSQL,在未添加修改源的情况下是PostgreSQL 13。本文采用默认安装,版本为PostgreSQL 13。

2023年9月14日,PostgreSQL 16正式发布。

二、安装

john@debian:~$ su
Password:

root@debian:/home/john# cat /etc/issue
Debian GNU/Linux 11 n l

root@debian:/home/john# apt update
...
root@debian:/home/john# apt install -y postgresql
...
root@debian:/home/john# systemctl status postgresql
... active(exited) ...
root@debian:/home/john#
三、配置

1、开启远程连接

修改pg_hba.conf和postgresql.conf文件,并重新启动服务。

修改配置前,监听127.0.0.1:5432,只能本地连接。

修改配置后,监听0.0.0.0:5432,可以远程连接。

john@debian:~$ su
Password:

root@debian:/home/john# ss -antl|grep 5432
LISTEN 0 244 127.0.0.1:5432 0.0.0.0:*
LISTEN 0 244 [::1]:5432 [::]:*
root@debian:/home/john# cd /etc/postgresql/13/main
root@debian:/home/john# vim pg_hba.conf
94 ...
95 # IPv4 local connections:
96 host all all 127.0.0.1/32 md5
97 host all all 0.0.0.0/0 md5
98 ...
:wq
root@debian:/home/john# vim postgresql.conf
59 ...
60 #listen_address = 'localhost'
61 listen_address = '*'
62 ...
:wq
root@debian:/home/john# systemctl restart postgresql
root@debian:/home/john# ss -antl|grep 5432
LISTEN 0 244 0.0.0.0:5432 0.0.0.0:*
LISTEN 0 244 [::]:5432 [::]:*
root@debian:/home/john# exit
john@debian:~$
四、使用

1、修改postgres用户的密码

先切换到root用户,再切换到postgres用户,再连接postgreSQL。linux的postgres用户可不使用密码直接登录postgresql的postgres用户。

john@debian:~$ su
Password:

root@debian:/home/john# su - postgres
postgres@debian:~$ psql
postgres=# ALTER USER postgres WITH PASSWORD '12345678';
postgres=# exit
postgres@debian:~$ exit
root@debian:/home/john# exit
john@debian:~$
2、创建新用户和数据库

用户名和库名须相同。

john@debian:~$ sql -h 192.168.1.100 -p 5432 -U postgres
Password for user postgres:
...
postgres=# create user db1 with password '12345678';
CREATE ROLE
postgres=# create database db1;
CREATE DATABASE
postgres=# quit
john@debian:~$
3、使用新用户创建表

john@debian:~$ psql -h 192.168.1.100 -p 5432 -U db1
...
Password for user db1:
db1=> create table emplayee (
db1(> id serial primary key,
db1(> name varchar(100)
db1(> );
CREATE TABLE
db1=>


原文链接:https://blog.yongit.com/note/1573121.html