Databases and web apps go together like peanut butter and jelly. In a word, they're inseparable. And despite all the amazing innovations in NoSQL data stores, often a good old relational database is the most reliable tool for the job.
We want to make it completely seamless to develop applications that need databases on Replit. Starting today, you can create and instantly begin to use PostgreSQL databases from within the Replit workspace.
Here's how you use it. First, click the PostgreSQL icon in the Tools pane on the lower left hand side of the workspace.
Or, you can open a new tab and select PostgreSQL from there.
From this pane, you can provision a PostgreSQL database. Compared to most SQL offerings which can take several minutes, your database on Replit will be available within just a few seconds.
For now we are only offering one size of database: 100 cycles per day for 10GB. The database has 1 dedicated CPU and 4 GB of RAM to process your queries.
Once you've created the database, you can start using it! The DATABASE_URL
environment variable will have your connection string. To connect to your database with NodeJS using the pg
package, you might use a code snippet like this:
const { Client } = require('pg')
const client = new Client(process.env.DATABASE_URL)
client.connect()
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
console.log(err ? err.stack : res.rows[0].message) // Hello World!
client.end()
})
We've also created a SQL explorer that you can use to write SQL statements and see the contents of your database.
If you've purchased Ghostwriter, AI-based code completion for SQL will be available in the statement editor.
For more information on your PostgreSQL database, check out:
Under the hood, this product is powered by our friends at Neon who have created a lightning-fast serverless database. The database will go to sleep after 5 minutes of inactivity. Most clients should handle the reconnection seamlessly.
In the near future we plan to offer much more flexibility and customizability in your database options. You'll be able to choose variable amounts of storage, different compute configurations, and even usage-based pricing, where you only pay for what you actually use.
If you experience any issues with your database at all, please post in the forum and we will get back to you as quickly as possible. Let us know what you think!