Using thirdparty Editor untuk PostgreSQL Server
Hai semuanya, Di materi kali ini kita akan membahas Third Party Editor yang bisa digunakan untuk berkomunikasi dengan PostgreSQL Server. Sepertinya kita ketahui ya PostgreSQL Server menggunakan TCP/IP dan Unix socket untuk berkomunikasi antara client dan server. Selain menggunakan aplikasi client bawaan, kita juga bisa menggunakan Aplikasi 3rd Party seperti
- Navicate
- Jetbraint DataGrip
- DBeaver
- HeidiSQL
- Dan masih banyak sekali editor lainnya
Nah jadi temen-temen bisa pilih salah satu dari 3rd party editor tersebut kalau mau pakai. Untuk saya sendiri biasanya menggunakan Editor IntelliJ IDEA buatnnya Jetbraint yang Ultimate.
Jadi di cource ini, kita akan menggunakan IntelliJ IDEA ya. Adapun pembahasannya kali ini kita akan setup environmentnya dulu mulai:
- Connection to PostgreSQL Server
- Database Tools
- Run a Query
- Manage database object
- Export/Import
- Diagrams ERD
Ok langsung aja kita ke pembahasan yang pertama
Connection to PostgreSQL Server
To issue a query to a database, you must create a data source connection. Data source is the location of your data and can be a server or a DDL file. The data source includes a name and connection settings that are dependent on the data source type.
-
In the Database tool window View > Tool Windows > Database, click the Data Source Properties icon The Data Source Properties icon.
-
On the Data Sources tab in the Data Sources and Drivers dialog, click the Add
+
(The Add icon) and select PostgreSQL. -
At the bottom of the data source settings area, click the Download missing driver files link. As you click this link, IntelliJ IDEA downloads drivers that are required to interact with a database. The IDE does not include bundled drivers in order to have a smaller size of the installation package and to keep driver versions up-to-date for each IDE version.
- From the Authentication list, select an authentication method.
- pgpass: by using the PGPASS file. You can store this file in the user’s home directory (for example,
/Users/jetbrains/.pgpass
). You can read more about the password file in The Password File at postgresql.org. - User & Password: by using your login and password.
- pgpass: by using the PGPASS file. You can store this file in the user’s home directory (for example,
-
Specify database connection details. Alternatively, paste the JDBC URL in the URL field.
- To ensure that the connection to the data source is successful, click the Test Connection link.
Berikut tampilannya:
Jika sudah kita bisa navigasi ke object databasenya seperti berikut:
Database Tools
The database management functionality in IntelliJ IDEA is supported by the Database tools and SQL plugin. The Database tools and SQL plugin provides support of all the features that are available in DataGrip, the standalone database management environment for developers. With the plugin, you can query, create and manage databases.
Databases can work locally, on a server, or in the cloud. The plugin supports MySQL, PostgreSQL, Microsoft SQL Server, SQLite, MariaDB, Oracle, Apache Cassandra, and others.
Banyak sekali feature yang di tawarkan oleh Database Tools plugin ini diantaranya
- Query results
- View two result sets in the editor
- Edit values in the result set
- Export to a file
- Export to a clipboard
- Sort data
- Filter data
- Write your code
- Code completion
- Generating code
- Manage database object
- Database
- User/Role
- Tables
- View
- etc
- Database diagrams
- Explain Plan: the result is shown in a mixed tree and table format on a dedicated Plan tab. You can click the Show Visualization icon (the Show Visualization icon) to create a diagram that visualizes the query execution.
- Explain Plan (Raw): the result is shown in a table format.
- Generate a diagram for a database object
- Save diagrams in the UML format
- Save diagrams in the PNG format
Run a Query
Query or database consoles are SQL files in which you can compose and execute SQL statements. Unlike scratch files, consoles are attached to a data source. You can use a query console in the same way as you use a terminal. Just type and run your code.
When you create a data source, a query console is created automatically. If necessary, you can create additional consoles. Each console creates a new connection. If you do not want to create new connections, enable single session mode.
To create a console, use one of the following actions in the Database tool window View > Tool Windows > Database.
- Click a data source and select File > New > Query Console.
- Right-click a data source and select New > Query Console.
- Click a data source, press
Alt+Insert
, and select Query Console. - Click a data source, press
Ctrl+Shift+F10
, and select New Query Console.
Selain itu juga kita bisa membuat file .sql
dengan cara Click menu File > New > File
Kemudian kita bisa pilih Change dialect > Project dialog & SQL Dialect > PostgreSQL seperti berikut:
Kemudian kita bisa jalankan Querynya dengan clik Play button warna hijau atau shortcutnya Ctrl + Enter
untuk windows/linux seperti berikut:
Nah klo perbedaanya menggunakan Query Console dan juga file adalah jika Query Console jika projectnya kita hapus maka akan hilang sedangkan menggunakan file, filenya akan tetap tersimpan jadi kita bisa jalankan di kemudian hari.
Manage database objects
The IDE provides a unified UI for adding or editing database object such as table, view, sequance, user/role, etc.
Export/Import schema
Being able to import and export data is useful when you move data between databases that are used for different purposes but need to share some data. For example, between development and production databases.
IntelliJ IDEA has an engine to export and import data in various formats. You can select a predefined format or create your own.
- Export: see how to generate DDL for objects and export data in different formats.
- Import: import CSV, TSV, or any other text files that contain delimiter-separated values. Also, learn how to restore the dump file by using
mysql
,pg_restore
, orpsql
. - Run migrations: learn how to use migration tools in IntelliJ IDEA.
- Data extractors: learn about default extractors and creating custom extractors.
Berikut contohnya, jika kita mau backup/export
Dan untuk import / restore seperti berikut:
ERD Diagram tools
Database diagrams graphically show the structure of the database and relations between database objects. You can generate a diagram for a data source, a schema, or a table. To create relations between database objects, consider using primary and foreign keys.
You can save the generated diagrams in two formats: UML and PNG. The UML format is an internal format developed specifically for IntelliJ IDEA. It is not supported by other products. If you want to share the created diagram, consider using PNG.