Could not find driver when connecting AWS RDS in Heroku - heroku

I have a database in AWS RDS but I keep getting error "could not find driver".
This error says that the driver is not enabled. But according to phpinfo() when viewed from my Heroku app, PDO driver for mysql is enabled. My local development via docker can connect and show data from my database in AWS RDS.
I'm expecting this should work fine because PDO mysql driver is showing enabled in Heroku. If AWS RDS does not allow this connection to Heroku, error message should not be about missing driver.
Code to connect to my database:
try {
$pdo = new PDO("mysql:example.amazonaws.com, "exampleuser", "examplepassword");
} catch (PDOException $e) {
die("Error: " . $e->getMessage());
}
The DSN and username/password are just example here, I don't want to provide my credentials but this set up works fine on my local development.

Related

AWS RDS: error: password authentication failed for user "ubuntu" from EC2

I have a postgres RDS instance which my Node.js web application running on an EC2 instance is not able to connect to. The error in my EC2 node logs is: error: password authentication failed for user "ubuntu"
I can confirm that I have the right username, password, database name, etc because it is working correctly on the development build on my machine. I copied all the .env parameters exactly into my ec2 machine for the production build. When attempting to connect to RDS on my production application web page, it fails. I have restarted my Node.js server multiple times and have rebooted the whole ec2 machine. I have confirmed that the env variables are there with printenv.
What would you recommend trying to fix this issue?
EDIT for more details: My nodejs setup should be correct because my nodejs server will call some external APIs that do not require my postgres database and those calls work properly.
EDIT2: This is strange because my username for RDS is postgres, while my username for EC2 is ubuntu. I wonder if somehow there's some clash between env variables. I checked printenv but didn't find any though
EDIT3: See comments for my workaround.
I would suggest to test the database credentials by directly connecting to RDS database using psql client on EC2 instance.

Connecting to SQL Server with pyodbc, FreeTDS, and Kerberos authentication on macOS

I never ask questions to forums, as I can generally find the answer somewhere on the interweb.
However, in this instance I cannot.
Summary: I can connect to and query the database with kerberos authentication via Azure Data Studio and tsql with FreeTDS. I cannot connect to the pyodbc. I've tried tens of different configurations with no success.
My ultimate goal is to connect to the MSSQL server DB with python.
Thank you for any input.
Background
local machine macOS 10.15.4
Connected to VPN required for kerberos authentication
Have successfully queried DB from Azure Data Studio
database is Microsoft SQL Server 2016
FreeTDS
tsql -S -U 'directory\username' -> Works, can query DB
isql
isql dsn_name 'directory\username' 'password'
error DIAG [42000] [FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
isql dsn_name 'directory\username'
error: DIAG [42000] [FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
pyodbc
cnxn = pyodbc.connect('DSN=dsn_name;Trusted_Connection=yes')
error:
pyodbc.ProgrammingError: ('42000', '[42000] [FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (18452) (SQLDriverConnect)')
cnxn = pyodbc.connect('DSN=dsn_name;UID=directory\username;PWD="password"')
error:
DIAG [01000] [FreeTDS][SQL Server]Adaptive Server connection failed
pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
Configuration
krb5.conf
libdefaults
default_realm = domain
[realms]
domain_same_as_default = {
kdc = kdc_address
}
odbc.ini
[dsn_name]
Description = MSSQL Server
Driver = FreeTDS
Servername = server_name
odbcinst.ini
[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
[ODBC]
Trace=Yes
TraceFile=/dev/stdout
freetds.conf
[server_name]
host = ip_address
port = port_num
database = db_name
REALM = DOMAIN
I've avoided using DSNs with pyodbc, as I prefer to have all my configuration in one spot. Here's an example connection string I use with a domain.
con = pyodbc.connect(
r"DRIVER={FreeTDS};"
r'SERVER=mssql.mydomain.com;'
r"PORT=1433;"
r"DATABASE=my_db;"
f"UID=MYDOMAIN\\my_username;"
f"PWD=my_password;"
r"TDS_Version=7.3;"
r"Encrypt=yes;"
r"Trusted_Connection=yes;"
)
Give that a whirl? The two backslashes (\\) are needed for escaping if using Windows domain auth, that is not a typo. The key thing you may be missing is TDS_Version. You can read more about TDS Versions here: https://www.freetds.org/userguide/ChoosingTdsProtocol.html
Good luck!

Unable to connect to Heroku Postgres from Play 2.3

I have a Play 2.3 app in Java. For this app, I am able to connect to local Postgres server.
But I am unable to connect to Heroku Postgres server.
I have following settings in my application.conf:
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://ec2test.amazonaws.com:5432/rwerwerwer"
db.default.user=eerwerwerer
db.default.password="5345fdwfdsvfvsdfsdfds"
I am getting following error:
Caused by: org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for ho
st "172.56.17.147", user "eerwerwerer", database "rwerwerwer", SSL off
From PgAdim III, I am able to successful connect to Heroku database.
While adding heroku config, I got:
**! No app specified.**
Follow these steps.
Things to remember:
Use Heroku commands from inside your main directory.
Use database connection as:
db.default.driver=org.postgresql.Driver
db.default.url=${?DATABASE_URL}
No need for username and password.
No need for Procfile.

Laravel Homestead- MySQL default credentials and database

I have setup Laravel and trying to run the artisan migrate command however I am getting the error below
[PDOException] SQLSTATE[HY000] [2002] Connection refused.
I am not sure how db is setup in Homestead. So I got the below questions.
Is default database created automatically by artisan migrate or Homestead?
If it is, what is the name of it?
If its not created by default, should we create before running migration?
I tried logging in to MySQL db by connecting to Homestead VM using ssh and then running MySQL. However I get error Access denied for user.... for user name vagrant, Homestead and forge.
What is the default credentials?
I understand that creating MySQL db is out of the scope of Laravel tutorial; So it would be helpful if anyone can answer these questions and point me in right direction.
Homestead comes with a default database called homestead. Your app can either choose to hook into that database, or you will have to go and make a new database manually. You can either use a GUI (like Sequel Pro on Mac) or perform it via the command line through Vagrant.
// SSH into the box
vagrant ssh
// Connect to MySQL as the homestead user (password is: secret)
mysql -u homestead -p
// Create a new database in MySQL
CREATE DATABASE your_app_name;
// Leave MySQL
exit;
You can then migrate the database as usual, php artisan migrate.
If you need to do this with Postgres instead, it's pretty similar.
// Connect to Postgres (password is: secret)
psql -U homestead -h localhost
// Create a new database in Postgres
CREATE DATABASE your_app_name;
// Leave Postgres
\q

Heroku Database Connection Properties

I'm trying to perform a relatively trivial task: I want to connect to a Heroku database. I have created the database and have been issued credentials from the Heroku site. However, when I try to connect to this database using anything besides the terminal 'heroku' command line client, I get fatal errors or cannot connect errors.
The two tools that I tried to connect with outside of the Heroku terminal application are: Navicat and IntelliJ.
The error that I receive in Navicat when trying to connect to the database is:
could not connect to server: Host is down
Is the server running on host "ec2-107-21-112-215.compute-1.amazonaws.com" and accepting
TCP/IP connections on port 5432?
My connection settings are as follows:
Connection Name Heroku Dev Test
Host Name/IP Address ec2-107-21-112-215.compute-1.amazonaws.com
Port 5432
Navicat doesn't even seem to be making an attempt to connect to that hostname.
When I try to connect with IntelliJ, using the full credentials, I get the following error:
java.sql.SQLException: FATAL: no pg_hba.conf entry for host "75.168.4.146", user "rphbqggxeokuxl", database "dc008iqk0rq4j5", SSL off
Again, I'm using the credentials that the Heroku application provides me with when accessing my database on their website.
Has anyone ran into this Heroku connection issue before?
I also had the issue with the FATAL: no pg_hba.conf entry for host error message.
I solved the connection issue to my Heroku Postgres database by adding the following to my JDBC string: &ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory.
Example
jdbc:postgresql://host:port/database?user=username&password=secret&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
You will need the SSL option only if SSL is enabled for your Postgres database (which is the default).
Hint
If you want to check your database connection properties, then just run the following command with the Heroku Toolbelt: heroku pg:info --app your-heroko-appname (make sure that you have Postgres insalled to run this command in your terminal)
The pg:info command will also tell you that sslmode is set to require.
To test the database connection I recommend SQL Power Architect as it is the tool which I was using to check my solution.
Heroku provides this information for connecting from external sources:
https://devcenter.heroku.com/articles/heroku-postgresql#external-connections-ingress
The second error message indicates PostgreSQL is not configured to accept the connection you're trying to make. Given the information Heroku provides, a good guess is that you're not connecting with SSL. Try to enable that on your connection.
Here are instructions for using SSL with Navicat: http://mirror.navicat.com/manual/online_manual/en/navicat/rv_manual/ClientCert.html.
This may be helpful in configuring Intellij to use SSL: https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely.
IntelliJ -> Datasources and Drivers
After you've configured the host, database and user details under the General tab switch to the Advanced tab and ensure that you've added the following:
ssl = true
sslfactory = org.postgresql.ssl.NonValidatingFactory
sslmode = require
You may consider setting a ENVIRONMENT CONFIG VARIABLE 'PGSSLMODE' to 'require' via Heroku's web interface or CLI.
Case: Postgres dB set up as a Heroku add-on and attached to app on a Heroku Dyno.
Heroku's instructions unfortunately leave out any mention of how to activate SSL, even though it is required for any dB tier starting with Standard-0 by default.
Follow all of the pg-copy or pg-upgrade steps (preferred approach depends on your version of Postgres) in Heroku instructions; however, before decommissioning the old database (if relevant) -- add the PGSSLMODE environment variable.
The instructions sufficiently cover how to promote the new database (and, consequently set the DATABASE_URL), so no changes/modifications to them should be required.
Wanted to help others who might run into this.
If you're supplying the Username and Password in seperate fields rather than on the command line, you need to use a ? between the database name and ssl=true and discard the first &
jdbc:postgresql://host:port/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
That's the command line that finally allowed me to connect to a PostgreSQL database using SQL Power Architect
For those who might be using Spring Boot and having the configuration provided through the DATABASE_URL environment property (not system property), the suffix can be added to the property:
?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
and passed through with a slight modification to the config bean:
#Bean
public BasicDataSource dataSource() throws URISyntaxException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
StringBuilder dbUrl = new StringBuilder(128);
dbUrl.append("jdbc:postgresql://")
.append(dbUri.getHost()).append(":")
.append(dbUri.getPort())
.append(dbUri.getPath());
String query = dbUri.getQuery();
if (null != query && !query.isEmpty()) {
dbUrl.append("?").append(query);
}
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(dbUrl.toString());
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
I'm using node.js and was trying to run my knex migrations for my Heroku app. I tried appending ?sslmode=require to the connection URL but it didn't work. I added ?ssl=true instead and now it works perfectly.
Here's an example Heroku PostgreSQL connection URL that works:
postgres://user:password#ec2-12-34-56-78.compute-99.amazonaws.com:port/databasename?ssl=true
Add or edit the following line in your postgresql.conf file :
listen_addresses = '*'
Add the following line as the first line of pg_hba.conf. It allows access to all databases for all users with an encrypted password:
TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 md5
Restart postgresql service:
net stop postgresql-9.0 & net start postgresql-9.0
(version should be based on your installation) -- On windows (run cmd as an administrator).
sudo service start postgresql -- On linux (or according to your linux distribution.)
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});

Resources