设置本地 PostgreSQL 数据库(Ubuntu)

166 阅读2分钟

上个月,我在 Vercel Postgres 上创建了一个数据库,用于存储野生动物保护网站中的数据。然而,免费版本在数据存储和传输方面存在每月限制。为了避免浪费在线资源,我决定搭建本地的 PostgreSQL 环境,以便于即将进行的网站开发调试工作。

安装 PostgreSQL

要安装 PostgreSQL 服务器,首先更新本地的软件包缓存,然后安装 postgresql 包:

sudo apt update
sudo apt install postgresql

登录 PostgreSQL

默认情况下,PostgreSQL 配置为使用“对等认证”(peer authentication),这意味着操作系统的用户名与 PostgreSQL 内部用户名相匹配时可以直接登录。

安装过程中创建了一个操作系统用户 postgres,它对应 PostgreSQL 中的管理员账号。要使用 psql 客户端登录 PostgreSQL,可以使用 sudo 以 postgres 用户身份运行以下命令:

sudo -u postgres psql

设置 PostgreSQL 用户密码

ALTER USER postgres PASSWORD 'yourpassword';
-- ALTER ROLE

使用连接 URI 连接 PostgreSQL

在为 postgres 用户设置密码后,你可以使用 URI 连接 PostgreSQL:

psql 'postgresql://postgres:yourpassword@localhost:5432/postgres?connect_timeout=10&sslmode=require'

更多详情参考:

www.prisma.io/docs/concep…

www.postgresql.org/docs/curren…

在 PostgreSQL 中创建表

CSV 文件 animals.csv 包含如下数据:

less animals.csv
# 1,Mammalia,Primates,Lorisidae,Nycticebus bengalensis,Bengal slow loris,蜂猴,I,EN,false,false,I
# 2,Mammalia,Primates,Lorisidae,Nycticebus pygmaeus,Pygmy slow loris,倭蜂猴,I,EN,false,false,I
# 3,Mammalia,Primates,Cercopithecidae,Macaca arctoides,Stump-tailed macaque,短尾猴,II,VU,false,false,
# ...

该表应包含以下列:

  • 动物 ID:唯一标识每只动物的编号
  • 纲(Class):动物所属的纲
  • 目(Order):动物所属的目
  • 科(Family):动物所属的科
  • 学名(Scientific Name):动物的学名
  • 常用名(Common Name):动物的英文常用名
  • 中文名(Chinese Name):动物的中文名
  • 保护等级(Protection Class):动物的国家保护等级
  • 保护状态(Conservation Status):动物的国际保护现状
  • 水生(Aquatic):是否为水生动物
  • 特有(Endemic):是否为特有物种
  • CITES:CITES 附录编号

使用以下 SQL 创建 animals 表:

CREATE TABLE IF NOT EXISTS animals (
  id serial PRIMARY KEY,
  class VARCHAR (50) NOT NULL,
  "order" VARCHAR (50) NOT NULL,
  family VARCHAR (50) NOT NULL,
  scientific_name VARCHAR (50) UNIQUE NOT NULL,
  common_name VARCHAR (50) NOT NULL,
  chinese_name VARCHAR (50) NOT NULL,
  protection_class VARCHAR (2) NOT NULL,
  conservation_status CHAR (2) NOT NULL DEFAULT 'DD',
  aquatic BOOLEAN NOT NULL DEFAULT FALSE,
  endemic BOOLEAN NOT NULL DEFAULT FALSE,
  cites VARCHAR(50)
);
-- CREATE TABLE

在 PostgreSQL 中导入 CSV 数据

你可以使用 COPY 命令轻松地将 CSV 导入 PostgreSQL:

\COPY animals
FROM '/path/xxx/animals.csv'
WITH csv;
-- COPY 186

查看数据表内容:

\d
--                List of relations
--  Schema |      Name      |   Type   |  Owner
-- --------+----------------+----------+----------
--  public | animals        | table    | postgres
--  public | animals_id_seq | sequence | postgres
-- (2 rows)

table animals;

--  id  |  class   |     order      |     family      |       scientific_name        |           common_name           |   chinese_name   | protection_class | conservation_status | aquatic | endemic |   cites
-- -----+----------+----------------+-----------------+------------------------------+---------------------------------+------------------+------------------+---------------------+---------+---------+-----------
--    1 | Mammalia | Primates       | Lorisidae       | Nycticebus bengalensis       | Bengal slow loris               | 蜂猴             | I                | EN                  | f       | f       | I

网站源码:github.com/shenlu89/am…

参考资料: