Connect to Your PostgreSQL Database
Connection information
To connect your applications to your Bridge PostgreSQL instance, get the connection information by accessing your instance in the dashboard.
-
Select the database cluster
-
View hostname and connection string
The hostname is always visible. You can also click “Show/Hide Credentials” to toggle displaying the connection string info
You’ll need to provide hostname, username, password, and database name. The default post number is 5432
. When your Bridge instance is provisioned, you can log in as the postgres
user to create more users and roles. It’s best practice to use postgres
only when necessary, so we recommend using another user with only the access permissions they need for your app connection.
Crunchy Bridge requires an SSL connection. Many clients will use SSL by default (but sometimes you may need to include the sslmode=require
parameter with the connection code).
Each example below assumes:
- basic knowledge of the language/framework
- the specified module or driver is installed before trying out the code samples
Python
Install the psycopg2
package.
You can then use this package to establish a database connection:
import psycopg2
conn = psycopg2.connect("dbname=testdb user=pythonapp password=securepassword host=p.s7d2ocnyg5bkvbllzckli7mwi4.db.postgresbridge.com")
Django
In a Django app, the database connection parameters are used in settings.py. The following is an example where the connection info is stored in a .env file:
DATABASE_URL=postgres://djangoapp:[email protected]m:5432/testdb
You could then use a package like dj_database_url
to provide the connection data in settings.py:
import dj_database_url
if os.environ.get('DATABASE_URL'):
DATABASES['default'] =
dj_database_url.config(default=os.environ['DATABASE_URL'])
Ruby
Ruby Pg is the Postgres module for Ruby.
require 'pg'
conn = PG.connect(
dbname: "testdb",
host: "p.s7d2ocnyg5bkvbllzckli7mwi4.db.postgresbridge.com"
user: "rubyapp",
password: "securepassword",
port: 5432
)
Ruby on Rails
To use your Bridge instance with a Rails app, you can include the database connection info in database.yml:
development:
adapter: postgresql
database: testdb
host: p.s7d2ocnyg5bkvbllzckli7mwi4.db.postgresbridge.com
username: railsapp
password: securepassword
Alternatively you can also store the connection info using environment variables.
Java
JDBC is a standard interface for connecting Java to Postgres.
import java.sql.Connection;
import java.sql.DriverManager;
String dbUrl = "jdbc:postgresql://p.s7d2ocnyg5bkvbllzckli7mwi4.db.postgresbridge.com/testdb?user=javaapp&password=securepassword&ssl=true";
Connection conn = DriverManager.getConnection(dbUrl);
Node.js
Install pg
, the Postgres client for Node.js.
const { Client } = require('pg');
const client = new Client({
user: 'nodeapp',
host: 'p.s7d2ocnyg5bkvbllzckli7mwi4.db.postgresbridge.com',
database: 'testdb',
password: 'securepassword',
port: 5432
});
client.connect();