尝试使用树莓派使用欧姆龙环境传感器(2JCIE-BU)。(2)

为了存储从2JCIE-BU中获取的各种数值,本次我们将在树莓派上安装并配置PostgreSQL。

安装PostgreSQL。

如果最初不假思索地运行sudo apt install postgres,就会遇到错误。

インストール失敗ログpi@raspberrypi:~ $ sudo apt install postgresql
パッケージリストを読み込んでいます… 完了
依存関係ツリーを作成しています…
状態情報を読み取っています… 完了
以下の追加パッケージがインストールされます:
libllvm7 libpq5 postgresql-11 postgresql-client-11 postgresql-client-common postgresql-common sysstat
提案パッケージ:
postgresql-doc postgresql-doc-11 libjson-perl isag
以下のパッケージが新たにインストールされます:
libllvm7 libpq5 postgresql postgresql-11 postgresql-client-11 postgresql-client-common postgresql-common sysstat
アップグレード: 0 個、新規インストール: 8 個、削除: 0 個、保留: 0 個。
27.0 MB のアーカイブを取得する必要があります。
この操作後に追加で 105 MB のディスク容量が消費されます。
続行しますか? [Y/n] y
取得:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libllvm7 armhf 1:7.0.1-8+rpi3+deb10u2 [11.4 MB]
エラー:2 http://raspbian.raspberrypi.org/raspbian buster/main armhf libpq5 armhf 11.12-0+deb10u1
404 Not Found [IP: 93.93.128.193 80]
エラー:4 http://raspbian.raspberrypi.org/raspbian buster/main armhf postgresql-client-11 armhf 11.12-0+deb10u1
404 Not Found [IP: 93.93.128.193 80]
エラー:6 http://raspbian.raspberrypi.org/raspbian buster/main armhf postgresql-11 armhf 11.12-0+deb10u1
404 Not Found [IP: 93.93.128.193 80]
取得:3 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf postgresql-client-common all 200+deb10u4 [85.1 kB]
取得:5 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf postgresql-common all 200+deb10u4 [225 kB]
取得:7 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf postgresql all 11+200+deb10u4 [61.1 kB]
取得:8 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf sysstat armhf 12.0.3-2 [513 kB]
12.2 MB を 5秒 で取得しました (2,281 kB/s)
E: http://raspbian.raspberrypi.org/raspbian/pool/main/p/postgresql-11/libpq5_11.12-0+deb10u1_armhf.deb の取得に失敗しました 404 Not Found [IP: 93.93.128.193 80]
E: http://raspbian.raspberrypi.org/raspbian/pool/main/p/postgresql-11/postgresql-client-11_11.12-0+deb10u1_armhf.deb の取得に失敗しました 404 Not Found [IP: 93.93.128.193 80]
E: http://raspbian.raspberrypi.org/raspbian/pool/main/p/postgresql-11/postgresql-11_11.12-0+deb10u1_armhf.deb の取得に失敗しました 404 Not Found [IP: 93.93.128.193 80]
E: いくつかのアーカイブを取得できません。apt-get update を実行するか –fix-missing オプションを付けて試してみてください

因此,我先执行了apt-get update,然后再试着运行apt install。

成功时的日志pi@raspberrypi:~ $ sudo apt-get update
获取:1 http://archive.raspberrypi.org/debian buster InRelease [32.6 kB]
获取:2 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
获取:3 http://archive.raspberrypi.org/debian buster/main armhf Packages [393 kB]
获取:4 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages [13.0 MB]
获取:5 http://raspbian.raspberrypi.org/raspbian buster/contrib armhf Packages [58.8 kB]
获取:6 http://raspbian.raspberrypi.org/raspbian buster/non-free armhf Packages [104 kB]
已获取 13.6 MB,在19秒内 (712 kB/s)
正在读取软件包列表… 完成
pi@raspberrypi:~ $ sudo apt install postgresql
正在读取软件包列表… 完成
正在建立依赖树
正在读取状态信息… 完成
下列额外的软件包将被安装:
libllvm7 libpq5 postgresql-11 postgresql-client-11
postgresql-client-common postgresql-common sysstat
建议安装:
postgresql-doc postgresql-doc-11 libjson-perl isag
下列新软件包将被安装:
libllvm7 libpq5 postgresql postgresql-11
postgresql-client-11 postgresql-client-common postgresql-common sysstat
升级了 0 个软件包,新安装了 8 个软件包,将卸载 0 个软件包,有 13 个软件包未被升级。
需要下载 27.0 MB/14.8 MB 的归档。
此操作后将新增 105 MB 的已使用空间。
您希望继续执行吗? [Y/n] y
获取:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libpq5 armhf 11.13-0+deb10u1 [155 kB]
获取:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf postgresql-client-11 armhf 11.13-0+deb10u1 [1,304 kB]
获取:3 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf postgresql-11 armhf 11.13-0+deb10u1 [13.3 MB]
已获取 14.8 MB,在7秒内 (2,117 kB/s)
正在预配置软件包 …
选择了以前未选择的软件包 libllvm7:armhf。
(正在读取数据库 … 目前共安装有 98850 个文件和目录。)
正在解压缩 libllvm7:armhf (1:7.0.1-8+rpi3+deb10u2) …
正在选择以前未选择的软件包 libpq5:armhf。
正在解压缩 libpq5:armhf (11.13-0+deb10u1) …
正在选择以前未选择的软件包 postgresql-client-common。
正在解压缩 postgresql-client-common (200+deb10u4) …
正在选择以前未选择的软件包 postgresql-client-11。
正在解压缩 postgresql-client-11 (11.13-0+deb10u1) …
正在选择以前未选择的软件包 postgresql-common。
正在解压缩 postgresql-common (200+deb10u4) …
正在选择以前未选择的软件包 postgresql-11。
正在解压缩 postgresql-11 (11.13-0+deb10u1) …
正在选择以前未选择的软件包 postgresql。
正在解压缩 postgresql (11+200+deb10u4) …
正在选择以前未选择的软件包 sysstat。
正在解压缩 sysstat (12.0.3-2) …
正在设置 postgresql-client-common (200+deb10u4) …
正在设置 libpq5:armhf (11.13-0+deb10u1) …
正在设置 postgresql-client-11 (11.13-0+deb10u1) …
update-alternatives: 使用 /usr/share/postgresql/11/man/man1/psql.1.gz 以提供 /usr/share/man/man1/psql.1.gz (psql.1.gz)
正在设置 postgresql-common (200+deb10u4) …
supported-versions: 警告!未知发行版:raspbian
在 ID_LIKE 中发现 debian,将其视为 Debian
将用户 postgres 添加到组 ssl-cert 中

正在创建配置文件 /etc/postgresql-common/createcluster.conf (新版本)
正在从已安装的 myspell/hunspell 软件包构建 PostgreSQL 词典 …
en_gb
正在删除过时的词典文件:
正在创建符号链接 /etc/systemd/system/multi-user.target.wants/postgresql.service → /lib/systemd/system/postgresql.service
正在设置 libllvm7:armhf (1:7.0.1-8+rpi3+deb10u2) …
正在设置 sysstat (12.0.3-2) …

正在创建配置文件 /etc/default/sysstat (新版本)
update-alternatives: 使用 /usr/bin/sar.sysstat 以提供 /usr/bin/sar (sar)
正在创建符号链接 /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service
正在设置 postgresql-11 (11.13-0+deb10u1) …
正在创建新的 PostgreSQL 集群 11/main …
/usr/lib/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main –auth-local peer –auth-host md5
数据库系统的文件所有者将是“postgres”。
此用户也应该拥有服务器进程。

数据库群集将以“ja_JP.UTF-8”区域设置初始化。
因此,默认的数据库编码方式已设置为UTF8。
initdb: 无法找到适用于区域“ja_JP.UTF-8”的合适的文本搜索设置
默认的文本搜索设置已设置为simple。

正在修正现有目录/var/lib/postgresql/11/main的权限 … 完成
正在创建子目录 … 完成
选择默认的 max_connections 值 … 100
选择默认的 shared_buffers 值 … 128MB
选择默认时区 … Asia/Tokyo
选择动态共享内存的实现 … posix
创建配置文件 … 完成
执行引导脚本 … 完成
执行引导后的初始化 … 完成
将数据同步到磁盘…完成

成功。您可以通过以下方式启动数据库服务器:

我按照成功安装时的提示信息执行了命令。

pi@raspberrypi:~ $ sudo pg_ctlcluster 11 main start
pi@raspberrypi:~ $ sudo su postgres
postgres@raspberrypi:/home/pi$ psql
psql (11.13 (Raspbian 11.13-0+deb10u1))
"help" でヘルプを表示します。

postgres=# exit
postgres@raspberrypi:/home/pi$ 

添加数据库用户

pi@raspberrypi:~ $ sudo su - postgres
postgres@raspberrypi:~$ createuser --pwprompt --interactive pgadmin
新しいロールのためのパスワード: 
もう一度入力してください:
新しいロールをスーパーユーザにしますか? (y/n)y
postgres@raspberrypi:~$ 

创建数据库

创建名为sample_db的数据库。

postgres@raspberrypi:~$ psql
psql (11.13 (Raspbian 11.13-0+deb10u1))
"help" でヘルプを表示します。

postgres=# \l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |     アクセス権限
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       |
 template0 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
(3 行)

postgres=# create database sample_db;
CREATE DATABASE

postgres=# \l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |     アクセス権限
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       |
 sample_db | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       |
 template0 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
(4 行)

postgres=#

创建桌子

作为样本,创建一个带有序列号和姓名的表格。
限制数据的状态不能同时为空(NULL)。

postgres@raspberrypi:~$ psql -U pgadmin -d sample_db -h localhost
ユーザ pgadmin のパスワード:
psql (11.13 (Raspbian 11.13-0+deb10u1))
SSL 接続 (プロトコル: TLSv1.3、暗号化方式: TLS_AES_256_GCM_SHA384、ビット長: 256、圧縮: オフ)
"help" でヘルプを表示します。

sample_db=# create table sample( id serial NOT NULL, name text NOT NULL, PRIMARY KEY(id) );
CREATE TABLE
sample_db=# \dt
            リレーション一覧
 スキーマ |  名前  |    型    |  所有者
----------+--------+----------+----------
 public   | sample | テーブル | postgres
(1 行)

sample_db=# \d
                 リレーション一覧
 スキーマ |     名前      |     型     |  所有者
----------+---------------+------------+----------
 public   | sample        | テーブル   | postgres
 public   | sample_id_seq | シーケンス | postgres
(2 行)

sample_db=# \d sample
                            テーブル "public.sample"
  列  |   型    | 照合順序 | Null 値を許容 |             デフォルト
------+---------+----------+---------------+------------------------------------
 id   | integer |          | not null      | nextval('sample_id_seq'::regclass)
 name | text    |          | not null      |
インデックス:
    "sample_pkey" PRIMARY KEY, btree (id)

sample_db=#

添加列

在中文中对以下语句进行改写:ALTER TABLE 表名 ADD COLUMN 列名 数据类型;

修改表 给 表名 添加 列名 数据类型。

sample_db=# ALTER TABLE sample ADD COLUMN date timestamp;
ALTER TABLE
sample_db=# \d sample
                                      テーブル "public.sample"
  列  |             型              | 照合順序 | Null 値を許容 |             デフォルト
------+-----------------------------+----------+---------------+------------------------------------
 id   | integer                     |          | not null      | nextval('sample_id_seq'::regclass)
 name | text                        |          | not null      |
 date | timestamp without time zone |          |               |
インデックス:
    "sample_pkey" PRIMARY KEY, btree (id)

删除列。

sample_db=# ALTER TABLE sample DROP COLUMN date;
ALTER TABLE
sample_db=# \d sample
                            テーブル "public.sample"
  列  |   型    | 照合順序 | Null 値を許容 |             デフォルト
------+---------+----------+---------------+------------------------------------
 id   | integer |          | not null      | nextval('sample_id_seq'::regclass)
 name | text    |          | not null      |
インデックス:
    "sample_pkey" PRIMARY KEY, btree (id)

PostgreSQL的配置

虽然我打算用一个树莓派来完成一切,所以觉得不需要,但还是提供给您。

只允许来自其他机器的访问

数据库设置文件postgre.conf

sudo vi /etc/postgresql/11/main/postgresql.conf 

因为有评论提到了listenaddresses,所以、、、

#listen_addresses = 'localhost'         # what IP address(es) to listen on;

请删除注释,并将地址从localhost更改为*。

listen_addresses = '*'         # what IP address(es) to listen on;

pg_hba.conf 的意思是 Postgres 数据库的主机访问规则文件。

pi@raspberrypi:~ $ sudo vi /etc/postgresql/11/main/pg_hba.conf 

为了允许从本地网络进行访问,需要添加一行代码。

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             192.168.10.0/24         md5

重新启动PostgreSQL

更新两个文件后,重新启动PostgreSQL。

pi@raspberrypi:~ $ sudo pg_ctlcluster 11 main restart

使用Python连接到PostgreSQL

将 pip3 更新到最新版本。

pi@raspberrypi:~ $ pip3 install --upgrade pip
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pip
  Downloading https://files.pythonhosted.org/packages/90/a9/1ea3a69a51dcc679724e3512fc2aa1668999eed59976f749134eb02229c8/pip-21.3-py3-none-any.whl (1.7MB)
    100% |????????????????????????????????| 1.7MB 234kB/s
Installing collected packages: pip
Successfully installed pip-21.3

安装psycopg2

pi@raspberrypi:~ $ pip3 install psycopg2
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting psycopg2
  Downloading https://www.piwheels.org/simple/psycopg2/psycopg2-2.9.1-cp37-cp37m-linux_armv7l.whl (505 kB)
     |????????????????????????????????| 505 kB 213 kB/s
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.1

使用Python连接到PostgreSQL。

我们将首先获取并显示在表中的数据,然后添加数据,最后再次获取并显示数据,以执行这个简单的示例。

import psycopg2

con = psycopg2.connect( database='sample_db', user='pgadmin', password='xxxxxx', host='localhost', port=5432)

cur = con.cursor();

cur.execute('SELECT * FROM sample;')
result = cur.fetchall()
print( result )

cur.execute("INSERT INTO sample(name) VALUES ('\"test02\"');" )

cur.execute('SELECT * FROM sample;')
result = cur.fetchall()
print( result )

con.commit()

cur.close()
con.close()

在样本表中,已经存在id:1,name:test01的数据。
在示例程序中,添加了name:test02。
运行示例后,id会自动增加,并添加test02。

pi@raspberrypi:~ $ python3 sample.py
[(1, '"test01"')]
[(1, '"test01"'), (2, '"test02"')]
pi@raspberrypi:~ $ psql -U pgadmin -d sample_db -h localhost -p 5432
ユーザ pgadmin のパスワード:
psql (11.13 (Raspbian 11.13-0+deb10u1))
SSL 接続 (プロトコル: TLSv1.3、暗号化方式: TLS_AES_256_GCM_SHA384、ビット長: 256、圧縮: オフ)
"help" でヘルプを表示します。

sample_db=# select * from sample;
 id |   name
----+----------
  1 | "test01"
  2 | "test02"
(2 行)
广告
将在 10 秒后关闭
bannerAds