dbeaver软件
安装postgresql
sudo apt-get update
sudo apt-get install postgresql postgresql-client
修改postgresql密码,创建数据库
psql -U postgres
ALTER USER postgres WITH PASSWORD 'new_password';
CREATE DATABASE DB_NAME;
\q
允许数据库被远程访问
点insert,加入这句到最后
host all all 0.0.0.0/0 md5
服务器没啥事了,现在到自己的电脑打开dbeaver
填写服务器ip,端口,用户名,密码
表就在这了
python远程连接数据库代码
import psycopg2 as pg
import random
import string
con = pg.connect(database="xxx", user="postgres", password="xx", host="x.x.x.x", port="5432")
cur = con.cursor()
def creat_tb():
cur.execute('''CREATE TABLE Users(
id INT primary key,
name varchar(20)
);''')
con.commit()
# creat_tb()
def fetch():
cur.execute("SELECT * from Users")
# rows = cur.fetchall()
print(cur.fetchall())
def clear():
cur.execute(f"TRUNCATE TABLE Users")
con.commit()
# clear()
def drop():
cur.execute(f"DROP TABLE Users")
con.commit()
def insert(id, name):
sql = "INSERT INTO Users (id, name) VALUES ('{}', '{}' )".format(id, name)
cur.execute(sql)
con.commit()
# insert(2, "Nix")
print(fetch())