I can't connect though a python script using psycopg2 to a postgresql server. I want to connect locally, through localhost. I can create a new server in the pgAdmin4 using the following parameters:
dbname=my-database
username=postgres
password=1234
host=localhost
port=5423
Here's the python code that doesn't work:
import psycopg2 as psy
def create_table():
conn = psy.connect(dbname="testdatabase", user="postgres", password="1234", host="localhost", port="5423")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS store(item TEXT, quantity INTEGER, price REAL)")
conn.commit()
conn.close()
def insert(item, quantity, price):
conn = psy.connect(dbname="testdatabase", user="postgres", password="1234", host="localhost", port="5423")
cur = conn.cursor()
cur.execute("INSERT INTO store VALUES(%s, %s, %s)", (item, quantity, price))
conn.commit()
conn.close()
def view():
conn = psy.connect("dbname='testdatabase' user='postgres' password='1234' host='localhost' port='5423'")
cur = conn.cursor()
cur.execute("SELECT * FROM store")
rows = cur.fetchall()
conn.close()
return rows
def delete(item):
conn = psy.connect("dbname='testdatabase' user='postgres' password='1234' host='localhost' port='5423'")
cur = conn.cursor()
cur.execute("DELETE FROM store WHERE item=(?)", (item,)) #need comma after when one item
conn.commit()
conn.close()
def update(item, price, quantity):
conn = psy.connect("dbname='testdatabase' user='postgres' password='1234' host='localhost' port='5423'")
cur = conn.cursor()
cur.execute("UPDATE store SET quantity=?, price=? WHERE item=?", (quantity, price, item))
conn.commit()
conn.close()
create_table()
insert("Golf stuff", 5, 2.65)
Here's my error:
Traceback (most recent call last):
File "psycopg.py", line 39, in <module>
create_table()
File "psycopg.py", line 4, in create_table
conn = psy.connect("dbname=testdatabase user=postgres password=1234 host=localhost port=5423")
File "C:\Users\Andy Renz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5423?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5423?
I've looked through many forums and tinkered with various files. Here's their status=>
pg-hba:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
postgresql (the relevant bits):
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
My hosts file:
# localhost name resolution is handled within DNS itself.
::1/ localhost
127.0.0.1/ localhost
Any help would be greatly appreciated! I'm pretty lost
Super super simple.
I wrote:
port=5423
It should be:
port=5432
Classic typo sending me to solve a problem that doesn't exist.
Thanks everyone who looked at my question!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.