N
Neon2y ago
rival-black

hello!

hello! just discover the neon platform, i've already setup everything in my account, it is starter account to learn and see if this will work for me. i'm trying to connect to the database using python and using the documentation, but every time i receive this error using python, node, pgadmin, anything that i've tried, same error: connection to server at "ep-long-grass-a4asj1iw.us-east-1.aws.neon.tech" (23.23.229.9), port 5432 failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. the variables connections its all right
17 Replies
blank-aquamarine
blank-aquamarine2y ago
Hey 👋🏻 Glad to have you trying out Neon! Can you share a script that you're using to connect? I think just the following would work in Python:
import os
import psycopg2
connection_string = "postgresql://neondb_owner:...@...-pooler.eu-central-1.aws.neon.tech/neondb?sslmode=require"
conn = psycopg2.connect(connection_string)
cur = conn.cursor()
cur.execute('SELECT NOW();')
time = cur.fetchone()[0]
cur.execute('SELECT version();')
version = cur.fetchone()[0]
cur.close()
conn.commit()
conn.close()
print('Current time:', time)
print('PostgreSQL version:', version)
import os
import psycopg2
connection_string = "postgresql://neondb_owner:...@...-pooler.eu-central-1.aws.neon.tech/neondb?sslmode=require"
conn = psycopg2.connect(connection_string)
cur = conn.cursor()
cur.execute('SELECT NOW();')
time = cur.fetchone()[0]
cur.execute('SELECT version();')
version = cur.fetchone()[0]
cur.close()
conn.commit()
conn.close()
print('Current time:', time)
print('PostgreSQL version:', version)
You'd need to install the psycopg2 python package: https://www.psycopg.org/docs/.
correct-apricot
correct-apricot2y ago
hello!! my code is the same that you send it, but still always receive the same error
blank-aquamarine
blank-aquamarine2y ago
Can you connect to the database using the following in your CLI: psql 'your-connection-string' [Notice the single quotes around the connection string]
correct-apricot
correct-apricot2y ago
i will try this, need install cli first still can't, don't know what i'm doing wrong
blank-aquamarine
blank-aquamarine2y ago
Interesting. Where are you copying the connection string from?
blank-aquamarine
blank-aquamarine2y ago
you'd just wanna use this whole thing as your connection string by using the "Copy snippet" button.
No description
correct-apricot
correct-apricot2y ago
i'm copying in this exact path, but seems not work, via python, node, django, already try a few codes, try on pgadmin too but always same error
correct-apricot
correct-apricot2y ago
No description
robust-apricot
robust-apricot2y ago
1. Do you get a different error in Node.js or psql? 2. Can you interact with your database using the SQL Editor on console.neon.tech? 3. If you visit Monitoring > System Operations on console.neon.tech do you see the database "start compute" when you attempt to connect using python or node?
psql 'postgresql://foo:bar@ep-nameless-water-a4sqimz3.us-east-1.aws.neon.tech/esihub?sslmode=require'
psql: error: connection to server at "ep-nameless-water-a4sqimz3.us-east-1.aws.neon.tech" (52.20.107.131), port 5432 failed: ERROR: password authentication failed for user 'foo'
psql 'postgresql://foo:bar@ep-nameless-water-a4sqimz3.us-east-1.aws.neon.tech/esihub?sslmode=require'
psql: error: connection to server at "ep-nameless-water-a4sqimz3.us-east-1.aws.neon.tech" (52.20.107.131), port 5432 failed: ERROR: password authentication failed for user 'foo'
I get an authentication failed error. Do you get that same error if you use an invalid username and password like I did?
correct-apricot
correct-apricot2y ago
1 - same error i get in both scenarios. 2 - yes i can, thats why i found this strange, on sql editor i can create table etc, but by code, pgadmin, i can't. 3 - the last start compute was 4 hours ago, i've already tried a few times after that the code python that i have now is:
import os
import psycopg2
from dotenv import load_dotenv

load_dotenv()

connection_string = os.getenv('DATABASE_URL')

conn = psycopg2.connect(connection_string)

cur = conn.cursor()

cur.execute('SELECT NOW();')
time = cur.fetchone()[0]
cur.execute('SELECT version();')
version = cur.fetchone()[0]

cur.close()
conn.close()

print('Current time:', time)
print('PostgreSQL version:', version)
import os
import psycopg2
from dotenv import load_dotenv

load_dotenv()

connection_string = os.getenv('DATABASE_URL')

conn = psycopg2.connect(connection_string)

cur = conn.cursor()

cur.execute('SELECT NOW();')
time = cur.fetchone()[0]
cur.execute('SELECT version();')
version = cur.fetchone()[0]

cur.close()
conn.close()

print('Current time:', time)
print('PostgreSQL version:', version)
import psycopg2
connection_string = "postgresql://esihub_owner:...@ep-nameless-water-a4sqimz3.us-east-1.aws.neon.tech/esihub?sslmode=require"
conn = psycopg2.connect(connection_string)
cur = conn.cursor()
cur.execute('SELECT NOW();')
time = cur.fetchone()[0]
cur.execute('SELECT version();')
version = cur.fetchone()[0]
cur.close()
conn.commit()
conn.close()
print('Current time:', time)
print('PostgreSQL version:', version)
import psycopg2
connection_string = "postgresql://esihub_owner:...@ep-nameless-water-a4sqimz3.us-east-1.aws.neon.tech/esihub?sslmode=require"
conn = psycopg2.connect(connection_string)
cur = conn.cursor()
cur.execute('SELECT NOW();')
time = cur.fetchone()[0]
cur.execute('SELECT version();')
version = cur.fetchone()[0]
cur.close()
conn.commit()
conn.close()
print('Current time:', time)
print('PostgreSQL version:', version)
and no, if i change the user, o don't get invalid username or pasword
No description
correct-apricot
correct-apricot2y ago
try this code too and try this one to
import psycopg2
from psycopg2 import OperationalError


db_name = "esihub"
db_user = "esihub_owner"
db_password = "..."
db_host = "ep-long-grass-a4asj1iw.us-east-1.aws.neon.tech"
db_port = "5432"


connection = None
cursor = None

try:

connection = psycopg2.connect(
dbname=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port
)


cursor = connection.cursor()


cursor.execute("SELECT version();")
db_version = cursor.fetchone()
print(f"Conectado ao banco de dados. Versão: {db_version}")

except OperationalError as error:
print(f"Erro ao conectar ao banco de dados: {error}")

except Exception as error:
print(f"Ocorreu um erro: {error}")

finally:

if cursor:
cursor.close()
if connection:
connection.close()
import psycopg2
from psycopg2 import OperationalError


db_name = "esihub"
db_user = "esihub_owner"
db_password = "..."
db_host = "ep-long-grass-a4asj1iw.us-east-1.aws.neon.tech"
db_port = "5432"


connection = None
cursor = None

try:

connection = psycopg2.connect(
dbname=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port
)


cursor = connection.cursor()


cursor.execute("SELECT version();")
db_version = cursor.fetchone()
print(f"Conectado ao banco de dados. Versão: {db_version}")

except OperationalError as error:
print(f"Erro ao conectar ao banco de dados: {error}")

except Exception as error:
print(f"Ocorreu um erro: {error}")

finally:

if cursor:
cursor.close()
if connection:
connection.close()
robust-apricot
robust-apricot2y ago
@Cordeiro this suggests to me that there might be something going on with your network setup. A user had a similar issue before, and using https://one.one.one.one/ fixed it for them. I don't know exactly what was the "real" issue with their network.
1.1.1.1 — The free app that makes your Internet faster.
Install the free app that makes your phone’s Internet more fast, private, and reliable.
correct-apricot
correct-apricot2y ago
hmmm, maybe is that the issue, maybe the firewall is blocking this, i will look into that thank you!
robust-apricot
robust-apricot2y ago
You're welcome, and sorry to hear you're having this issue! It's potentially firewall or DNS 👍
correct-apricot
correct-apricot2y ago
yes! i let you know later if i solve this, thanks it was exactly that, firewall, now i can connect!
blank-aquamarine
blank-aquamarine2y ago
Ooh interesting, a learning for me as well.
robust-apricot
robust-apricot2y ago
Excellent! I'm glad you're unblocked 🙂

Did you find this page helpful?