a remote connection
不论你连接的是本地的┞氛样长途的数据库,点击打开 Server Groups > Servers > test > Schemas > public > Tables,右键单击 people 表,然后选择 View Data > View top 100 Rows。你如今将看到你前面输入的数据。
viewing test data
你如今可以创建和修改数据库和表、治理用户,和应用 GUI 炔源溷本身的萌芽了。你可能会发明这种可视化办法比敕令行更易于治理。
Python
要年腋荷琐 Python 脚本连接到你的数据库,你将须要 Psycopg2 这个 Python 包。你可以用 pip 来安装它:
- sudo pip3 install psycopg2
如今打开一个 Python 编辑器写一些代码连接到你的数据库:
- import psycopg2
- conn = psycopg2.connect('dbname=test')
- cur = conn.cursor()
- cur.execute('select * from people')
- results = cur.fetchall()
- for result in results:
- print(result)
如不雅你想去拜访树莓派,你可以用 apt 去安装它:
运行这个代码去看萌芽结不雅。留意,如不雅你连接的是长途数据库,在连接字符串中你将须要供给更多的凭证,比如,增长主机 IP、用户名,和数据库暗码:
- conn = psycopg2.connect('host=192.168.86.31 user=pi
- password=raspberry dbname=test')
你甚至可以创建一个函数去运行特定的萌芽:
- def get_all_people():
- query = """
- SELECT
- *
- FROM
- people
- """
- cur.execute(query)
- return cur.fetchall()
和一个包含参数的萌芽: