简体   繁体   English

使用 Linux 和 PGAdmin 设置可远程访问的 Postgres 数据库

[英]Setting up a Remotely Accessible Postgres Database with Linux and PGAdmin

I'm trying to set up a remotely accessible Postgres database .我正在尝试建立一个可远程访问的 Postgres 数据库 I want to host this databse on one Linux based device (HOST), and to access it on another Linux based device (CLIENT).我想在一个基于Linux的设备 (HOST) 上托管这个数据库,并在另一个基于 Linux 的设备 (CLIENT) 上访问它。

In my specific case, HOST is a desktop device running Ubuntu.在我的具体情况下,HOST 是运行 Ubuntu 的桌面设备。 CLIENT is a Chromebook with a Linux virtual system. CLIENT 是带有 Linux 虚拟系统的 Chromebook。 (I know. But it's the closest thing to a Linux based device that I have to hand. (我知道。但它是我手头最接近基于 Linux 的设备的东西。

Steps Already Taken to Set Up the Database已采取的设置数据库的步骤

  1. Installed the required software on HOST using APT.使用 APT 在 HOST 上安装了所需的软件。
PGP_KEY_URL="https://www.postgresql.org/media/keys/ACCC4CF8.asc"
POSTGRES_URL_STEM="http://apt.postgresql.org/pub/repos/apt/"
POSTGRES_URL="$POSTGRES_URL_STEM `lsb_release -cs`-pgdg main"
POSTGRES_VERSION="12"
PGADMIN_URL_SHORT="https://www.pgadmin.org/static/packages_pgadmin_org.pub"
PGADMIN_URL_STEM="https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt"
PGADMIN_TO_ECHO="deb $PGADMIN_URL_STEM/`lsb_release -cs` pgadmin4 main"
PGADMIN_PATH="/etc/apt/sources.list.d/pgadmin4.list"

sudo apt install curl --yes
sudo apt install gnupg2 --yes
wget --quiet -O - $PGP_KEY_URL | sudo apt-key add -

echo "deb $POSTGRES_URL" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt install postgresql-$POSTGRES_VERSION --yes
sudo apt install postgresql-client-$POSTGRES_VERSION --yes

sudo curl $PGADMIN_URL_SHORT | sudo apt-key add
sudo sh -c "echo \"$PGADMIN_TO_ECHO\" > $PGADMIN_PATH && apt update"
sudo apt update
sudo apt install pgadmin4 --yes
  1. Create a new Postgres user.创建一个新的 Postgres 用户。
NU_USERNAME="my_user"
NU_PASSWORD="guest"
NU_QUERY="CREATE USER $NU_USERNAME WITH superuser password '$NU_PASSWORD';"

sudo -u postgres psql -c "$NU_QUERY"
  1. Created the new server and database.创建了新的服务器和数据库。 I did this manually, using the PGAdmin GUI.我使用 PGAdmin GUI 手动完成了这项工作。
  2. Added test data, a table with a couple of records.添加了测试数据,一个包含几条记录的表。 I did this with a script.我用脚本做到了这一点。
  3. Followed the steps given in this answer to make the databse remotely accessible.按照此答案中给出的步骤使数据库可以远程访问。

Steps Already Taken to Connect to the Database REMOTELY远程连接数据库的步骤

  1. Installed PGAdmin on CLIENT.在客户端上安装了 PGAdmin。
  2. Attempted to connect using PGAdmin.尝试使用 PGAdmin 进行连接。 I used the "New Server" wizard, and entered:我使用“新建服务器”向导,并输入:

Host IP Address: 192.168.1.255主机IP地址:192.168.1.255
Port: 5432 (same as when I set up the database on HOST)端口:5432(和我在HOST上设置数据库时一样)
User: my_user用户:my_user
Password: guest密码:访客

However, when I try to save the connection, PGAdmin responds after a few seconds saying that the connection has timed out .但是,当我尝试保存连接时,PGAdmin 会在几秒钟后回复说连接已超时

You have to configure listen_addresses in /var/lib/pgsql/data/postgresql.conf like this:您必须像这样在/var/lib/pgsql/data/postgresql.conf中配置listen_addresses

listen_addresses = '*'

Next make sure your firewall doesn't block the connection by checking if telnet can connect to your server:接下来通过检查 telnet 是否可以连接到您的服务器来确保您的防火墙不会阻止连接:

$ telnet 192.168.1.255 5432
Connected to 192.168.1.255.
Escape character is '^]'.

If you see Connected network connectivity is ok.如果您看到Connected的网络连接正常。 Next you have to configure access rights for remote hosts.接下来,您必须配置远程主机的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM