joplin-notes/Export-9fad9947-5bd5-4195-952b-6512c5a4beb1/Web development 83dd4e6675f04ba782a6865a434a0598/Heroku 531d47e94b8843d7a0dc8ffce482bf9e/Connecting with PostgreSQL Database 27806e96bc2941.md
2026-11-04 00:33:23 +05:30

80 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Connecting with PostgreSQL Database
- Local important commands for exporting and importing
### Exporting postgresql Database
We need to create a dump file. Here *smart-brain* is name of database
`pg_dump -U postgres smart-brain > dump.sql`
### Importing postgresql Database
`psql -U postgres smart-brain < dump.sql`
For using insert into instead of copy: [https://stackoverflow.com/questions/2827072/sql-convert-backup-file-from-copy-format-to-insert-format](https://stackoverflow.com/questions/2827072/sql-convert-backup-file-from-copy-format-to-insert-format)
`pg_dump --column-inserts -U <dbuser> <database> > dumpWithInserts.sql`
for railway cloud:
[https://blog.railway.app/p/postgre-backup](https://blog.railway.app/p/postgre-backup)
The above may not work well in windows so we can use the pgadmin which is workbench. To create dump file use backup functionality with binary encoding. This dump file can be easily imported using restore functionality.
Some extra functionality like:
To get list of tables: `\d`
to get schema of table ‘users’: `\d users;`
### Importing dump file to heroku created by pgadmin
dump file to be imported to heroku must be publically accessible http url. Dropbox is recommended and remove letter ‘s’ from https(https→ http).
`heroku pg: backups: restore ‘http://…..’`
- Configuration for postgreSQL in nodejs for production mode:
```jsx
const knex = require("knex")({
client: "pg",
connection: {
// host: "127.0.0.1",
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false },
},
});
```
This database_url is to be uploaded in heroku config file section under settings when navigated to the particular app.Refer to the image:
![heroku env.png](../../../_resources/heroku%20env.png)
This database_url can be accessed by using the heroku command:
heroku pg:info
![heroku database_url.png](../../../_resources/heroku%20database_url.png)
This add-on property contains databse url.
### Accessing database through command prompt:
`heroku pg:psql -a face-extractor-app`