Implement PostgreSQL In Flutter
Hello Everyone!!! Today we learn about PostgreSQL With Flutter In this article, we cover topics like how to set up PostgreSQL and also how we can use PostgreSQL With Flutter.
If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.
Table Of Contents
Introduction of PostgreSQL:
PostgreSQL is a powerful, open-source object-relational database system. It has more than 15 years of active development phase and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.
PostgreSQL (pronounced as post-gress-Q-L) is an open-source relational database management system (DBMS) developed by a worldwide team of volunteers. PostgreSQL is not controlled by any corporation or other private entity and the source code is available free of charge.
PostgreSQL Setup Steps:
- Downloading the installer
- Launching the installation
- Selecting the install location
- Selecting components
- Selecting where to store data
- Setting the user password
- Selecting the port number
- Setting locale
- Review and installation
1. Downloading the installer
Visit the downloads page of EnterpriseDB’s website, https://www.enterprisedb.com/downloads/postgres-postgresql-downloads. Download your preferred version from the list available.
2. Launching the installation
Run the downloaded dmg package as the administrator user. When you get the screen below, click on the “Next” button:
3. Selecting the install location
You will be asked to specify which directory you wish to use to install Postgres. Select your desired location and click “Next”:
4. Selecting components
You will next be asked to select the tools that you want to install along with the Postgres installation. PostgreSQL server and command line tools are compulsory. Stack Builder and pgAdmin 4 are optional. Please select from the list and click “Next”:
5. Selecting where to store data
You will be asked to select the location for your Postgres cluster’s Data Directory. Please select an appropriate location and click “Next”:
6. Setting the superuser password
You will be asked to provide the password of the Postgres Unix superuser, which will be created at the time of installation. Please provide an appropriate password and click “Next”:
7. Selecting the port number
You will be asked to select the port number on which the PostgreSQL server will listen for incoming connections. Please provide an appropriate port number. (The default port number is 5432.) Make sure the port is open from your firewall and the traffic on that port is accessible. Click “Next”:
8. Setting locale
Please select the appropriate locale (language preferences) and click “Next”:
9. Review and installation
You will be provided a summary of your selections from the previous installation screens. Review it carefully and click “Next” to complete the installation:
Add Dependency:
Run this command:
With Dart:
$ dart pub add postgres
With Flutter:
$ flutter pub add postgres
This will add a line like this to your package’s pubspec.yaml (and run an implicit dart pub get
):
dependencies:
postgres: ^2.6.1
Alternatively, your editor might support dart pub get
or flutter pub get
. Check the docs for your editor to learn more.
Import it
Now in your Dart code, you can use:
import 'package:postgres/postgres.dart';
Configuration:
Create PostgreSQLConnection
and open
them:
static var connection = PostgreSQLConnection(
Constants.dbHostForEmulator, Constants.port, Constants.db,
username: Constants.dbUsername, password: Constants.dbPassword);
static Future<void> requestForDBConnectionStart() async {
await connection.open().then((value) => debugPrint("Connection Establish"));
}
Execute queries with query
:
Queries for Creating Tablestatic Future<String?> createTable() async{
try {
await connection.query('''
CREATE TABLE person(
name text,
email text
)
''').then((value) => (){
return "table created successfully";
});
} on PostgreSQLException catch (e) {
print(e.message);
return e.message;
}
return "";
}
Queries for Insert data in the table
static Future<void> addData() async {
try {
await connection.query('''
INSERT INTO person(name,email)
VALUES ('RAHUL THAKUR','rahul@oppong.co')
''');
} on PostgreSQLException catch (e) {
print(e.message);
}
}
Queries for fetching all data of the given table
static Future<void> fetchAllData() async {
try {
dynamic results = await connection.query("SELECT * FROM ${Constants.usersTable}");
if (results.isEmpty) {
print("No Record Found");
} else {
for (final row in results) {
var a = row[0];
var b = row[1];
print("A = " + a);
print("B = " + b);
}
}
} on PostgreSQLException catch (e) {
print(e.message);
}
}
Queries for closing the DB connection
static Future<void> requestForDBConnectionStop() async {
try {
await connection
.close()
.then((value) => debugPrint("Connection successfully close"));
} on PostgreSQLException catch (e) {
print(e.message);
}
}
Conclusion:
In the article, I have explained the implementation of PostgreSQL In Flutter; you can modify this code according to your choice. This was a small introduction to the implementation of PostgreSQL In Flutter User Interaction from my side, and it’s working using Flutter.
I hope this blog will provide you with sufficient information on Trying to Implement PostgreSQL In Flutter in your Flutter projects. We will show you what the Introduction is. Make a demo program for working on PostgreSQL with Flutter in your Flutter applications. So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
GitHub Link:
You can check out Implement PostgreSQL In Flutter on GitHub. We hope you enjoyed this tutorial
GitHub – rahul-t-aeologic/postgres_with_flutter_demo
Contribute to rahul-t-aeologic/postgres_with_flutter_demo development by creating an account on GitHub.github.com
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.